I am trying to call Action method passing a single parameter.
The method (Accepts Http Posts) is being hit when the submit button is clicked (following a dialog popped via java script for the user to confrim). However the value passed into the action method is alway null. Below is a snippet of the Page source rendered to the browser. This shows two typical rows. There are no other 'Form' elements on the page.
Any ideas as to why the value is not passed.
Thanks
Grant
<tr class="inactive-row">
<td>Title1</td>
<td></td>
<td>01 January 2010</td>
<td>
<form action="/Admin/Delete/1" method="post" onsubmit="return confirmDelete()">
<input type='image' src='/Content/delete.gif' value='delSubmit' alt='Delete' />
</td>
</tr>
<tr class="inactive-row">
<td>Title2</td>
<td></td>
<td>01 January 0001</td>
<td>
<form action="/Admin/Delete/2" method="post" onsubmit="return confirmDelete()">
<input type='image' src='/Content/delete.gif' value='delSubmit' alt='Delete' />
</td>
</tr>
Thanks for you help. Below is the global.asax code:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}
AdminController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using System.Xml.XPath;
using DataGridInMVC.Models;
namespace DataGridInMVC.Controllers
{
public class AdminController : Controller
{
private XDocument document;
//
// GET: /Admin/
public ActionResult Index(int ? PageNumber)
{
var data = GetPostData(PageNumber);
//Build the model and then Sent to the View
return View(data);
}
private MyAdminDataModel GetPostData(int? pageNumber)
{
document = XDocument.Load(Server.MapPath("~/App_Data/Posts.xml"));
var elList = from el in document.Descendants("Post")
select new Page { ID = (string)el.Attribute("ID"), PermaLink=(string)el.Attribute("PermaLink"), Title = (string)el.Attribute("Title") };
return new MyAdminDataModel { CurrentPage = pageNumber ?? 0, Pages = elList, TotalPages = 2 };
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(int ? delId)
{
document.XPathSelectElement(String.Format("Posts/Post[@ID = '{0}']",delId )).Remove();
document.Save(Server.MapPath("~/App_Data/Posts.xml"));
var model = GetPostData(null);
return View("Index",model);
}
}
}
The Extension methods for htmlHelper:
public static string DeleteForm(this HtmlHelper helper, string controller, string action, int id)
{
//UrlHelper url = new UrlHelper(helper.ViewContext);
UrlHelper url = new UrlHelper(helper.ViewContext.RequestContext);
string postAction = url.Action(action, controller, new { id = id });
string formFormat = "<form action=\"{0}\" method=\"post\" onsubmit=\"return confirmDelete()\"/>";
return string.Format(formFormat, postAction);
}
public static string SubmitImage(this HtmlHelper helper, string imageName, string imageFile, string altText)
{
return string.Format("<input type='image' src='{0}' value='{1}' alt='{2}' />", imageFile, imageName, altText);
}
The code I have is from Rob Conery's Blog http://blog.wekeroad.com/blog/asp-net-mvc-avoiding-tag-soup/