ASP.MVC参数未传递给http post的Action方法

时间:2010-06-03 22:03:45

标签: c# asp.net-mvc asp.net-mvc-2

我试图调用Action方法传递一个参数。

单击提交按钮时会触发方法(接受Http Posts)(通过java脚本弹出一个对话框供用户进行confrim)。但是,传递给action方法的值总是为null。下面是呈现给浏览器的页面源代码片段。这显示了两个典型的行。页面上没有其他“表单”元素。

关于为什么没有传递价值的任何想法。

由于

捐赠

<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>

谢谢你的帮助。下面是global.asax代码:

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);
        }
    }
}

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);
}

我的代码来自Rob Conery的博客 http://blog.wekeroad.com/blog/asp-net-mvc-avoiding-tag-soup/

1 个答案:

答案 0 :(得分:3)

在上面的代码中,我认为您错过了form代码的结束标记。

编辑:您不希望使用Html.ActionLink而不是提交按钮的任何原因,因为您似乎不需要提交任何表单数据并且{{1}光很好吗?

编辑2 :尝试将控制器ActionLink方法定义更改为:

Delete

我认为它无法正确匹配params。我刚刚测试了你的代码,而param名称肯定是问题所在。我能够重现同样的问题,但重命名为public ActionResult Delete(int? id) 解决了它。