开发者

How to send and retrieve in the controller

I have the folowing code in my view, however, I can see that I don`t have the values in the controller. What is wrong? In the view I have,

<%
    using (Html.BeginForm())
    {%>
       <%=Html.TextBox("Addresses[0].Line1") %>
       <%=Html.TextBox("Addresses[0].Line2")%>
       <%=Html.TextBox("Addresses[1].Line1")%>
       <%=Html.TextBox("Addresses[1].Line2")%>

        <input type="submit" name="submitForm" value="Save products" />
        <%
    }
%>

My classes are as follows:

public class Customer
{
    public string FirstName { get; set; }
    public string Lastname { get; set; }
    public List<Address> Addresses { get; set; }
    public Customer()
    {
        Addresses = new List<Address>();
    }
}

public class Address
{
    public int Lin开发者_如何学Ce1 { get; set; }
    public int Line2 { get; set; }
}

My controller as follows:

public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(Customer customer)
    {
        return View();
    }


The parameter for your ActionResult is named customer, so the default model binder will be looking for that name in the form by default. I believe if you modify your code to the following it should pick it up:

   <%=Html.TextBox("customer.Addresses[0].Line1") %>
   <%=Html.TextBox("customer.Addresses[0].Line2")%>
   <%=Html.TextBox("customer.Addresses[1].Line1")%>
   <%=Html.TextBox("customer.Addresses[1].Line2")%>


Check to ensure your View is bound to the Customer model.

Also, when viewing the web page containing the form, view the source generated by the View to see if the fields are being properly named.

Finally, if none of the above helps, change the parameter in your Index action like so:

public ActionResult Index(FormCollection form)

then you can use the debugger to inspect the FormCollection object that is passed in to see exactly what the View is sending you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜