开发者

ASP.NET MVC2 Post button has right values but doesn't send values to action

I have a button who's code is generated like this:

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

The actual code generated:

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

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

But the buttons calls this address:

http://localhost:20588/Customers/EventEdit

and I get开发者_如何学JAVA:

The parameters dictionary contains a null entry for parameter 'EventID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult EventEdit(Int32, Int32)' in 'TechRun.UI.Controllers.CustomersController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

Any Idea what am I doing wrong (there are other post buttons on that form, but they work ok). Thanks.


Try like this:

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

Also make sure that there's no some javascript that could interfere with the form submission sending an AJAX request and forgetting to include the values. And finally make sure that Model.customer.CustomerID is not empty. Use FireBug to see exactly what is the request being sent to the server.


UPDATE:

According to the specification:

If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type.

This means that you should not use query string parameter in the form action with GET method. You need to use hidden fields inside the form to pass those values to the server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜