2011-01-07 38 views
0

我已经是这样产生的一个按钮谁的代码:ASP.NET MVC2邮政按钮有正确的价值观,但不发送值行动

<% RouteValueDictionary dictionary2 = new RouteValueDictionary(); %> 
     <% dictionary2.Add("EventID",0); %> 
     <% dictionary2.Add("CustomerID",Model.customer.CustomerID.ToString()); %> 
     <% using (Html.BeginForm("EventEdit", "Customers", dictionary2,FormMethod.Get,null)) 
        { %> 
     <button type="submit"> 
      new event</button> 
     <%} %> 

实际的代码生成:

<form action="/Customers/EventEdit?EventID=0&amp;CustomerID=1" method="get"> 

       <button type="submit"> 
        new event</button> 
       </form> 

但按键调用该地址:

http://localhost:20588/Customers/EventEdit

,我也得到:

参数字典包含参数 无效项“System.Int32”的 法“的 非可空类型“事件ID” System.Web.Mvc.ActionResult EventEdit (Int32,Int32)' 'TechRun.UI.Controllers.CustomersController'。 可选参数必须是 参考类型,可为空的类型,或者将其声明为可选参数 。 参数名称:参数

任何想法我做错了什么(该窗体上有其他后按钮,但他们工作正常)。 谢谢。

+0

使用调试器查看存在的值。 – leppie 2011-01-07 11:06:12

+0

我检查页面的源代码,也使用萤火虫 - 我得到正确的代码:/ Customers/EventEdit?EventID = 0 & CustomerID = 1。我已经拿到了这些值并将它们粘贴到地址栏上 - 并且它工作正常。 – Dani 2011-01-07 11:11:15

回答

1

尝试 like this

<% using (Html.BeginForm(
    "EventEdit", 
    "Customers", 
    new { EventId = "0", CustomerID = Model.customer.CustomerID }, 
    FormMethod.Get)) 
{ %> 
    <input type="submit" value="new event" /> 
<% } %> 

还要确保有没有一些JavaScript可与表单提交Ajax请求,并忘记包括价值观干扰。最后确保 Model.customer.CustomerID不是空的。使用FireBug精确地查看发送到服务器的请求。


UPDATE:

按照specification

如果该方法是 “获取” 和动作 是HTTP URI,用户代理采取 动作的值,附加一个'?'到 它,然后附加表单数据集 编码使用 “application/x-www-form-urlencoded” 内容类型。

这意味着您不应该在GET方法的表单action中使用查询字符串参数。您需要使用表单中的隐藏字段将这些值传递给服务器。