views:

270

answers:

1

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/

+1  A: 

In the above code I think you're missing closing tags for your form tags.

EDIT: Any reason you don't want to use Html.ActionLink instead of a submit button since it looks like you don't need any form data submitted and an ActionLink is nice a light?

EDIT 2: Try changing your controller Delete method definition to:

public ActionResult Delete(int? id) 

I think it can't match up the params properly. I just tested your code and the param name is definately the problem. I was able to reproduce the same issue but renaming it to id solved it.

Kelsey
Thanks. You were correct in terms the closing tag, but it is still behaving the same way.
Grant Sutcliffe
@Grant Sutcliffe try an ActionLink instead of a button and see if that works. If it doesn't there is something else wrong with how your handling the request.
Kelsey
@Grant Sutcliffe also see the param names of your controller.
Kelsey
Brilliant, changing. Your suggestion to change the Delete definition solved it. I understand now that I need to match the parameter name with that in the view.Namely: string postAction = url.Action(action, controller, new { id = id });Will also change to use ActionLink - as per your other suggestion.Thanks Kelsey.
Grant Sutcliffe
@Grant Sutcliffe if this helped you answer your question you should mark this as the accepted answer. Thanks glad I could help.
Kelsey