MVC UpdateModel ComplexType

大城市里の小女人 提交于 2019-12-06 05:09:02

问题


If you have the following type.

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<TheParameters> Parameters { get; set; }
    public Address Address { get; set; }
}
public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}
public class TheParameters
{
    public string Parameter { get; set; }
}

You make your page stronglytyped to Person.

"System.Web.Mvc.ViewPage<Person>"

<form action="/Home/Save" method="post">  

    <b>Your Name</b> 
    <label for="FirstName">  
        <span>First Name</span>  
        <%=Html.TextBox("Person.FirstName", ViewData.Model.FirstName) %>  
    </label>  

    <label for="LastName">  
        <span>Last Name</span>  
        <%=Html.TextBox("Person.LastName", ViewData.Model.LastName)%>  
    </label>  

    <b>Your Address</b>  

    <label for="Street">  
        <span>Street</span>  
        <%=Html.TextBox("Person.Address.Street", ViewData.Model.Address.Street)%>  
    </label>  

    <label for="City">  
        <span>City</span>  
        <%=Html.TextBox("Person.Address.City", ViewData.Model.Address.City)%>  
    </label>  

    <label for="State">  
        <span>State</span>  
        <%=Html.TextBox("Person.Address.State", ViewData.Model.Address.State)%>  
    </label>  

    <label for="Parameters">
        <span>Parameters</span>
        <%
            int index = 0;
            foreach (TheParameters parameter in ViewData.Model.Parameters)
            {
                Response.Write(Html.TextBox("Person.Parameters.Parameter[" + index + "]", parameter.Parameter));
                index++;
            }
         %>
    </label>

    <input id="submit" type="submit" value="submit" />  

</form> 

In the controller the following:

    public ActionResult Index()
    {
        Person p = new Person();
        p.FirstName = "Name";
        p.LastName = "Last";
        p.Address = new Address();
        p.Address.City = "city";
        p.Address.State = "state";
        p.Address.Street = "street";

        p.Parameters = new List<TheParameters>();
        p.Parameters.Add(new TheParameters(){ Parameter = "P1" });
        p.Parameters.Add(new TheParameters(){ Parameter = "p2" });

        ViewData.Model = p;

        return View();
    }

    public ActionResult Save(FormCollection form)   
    {
        Person p = new Person();


        UpdateModel(p, "Person", form.ToValueProvider());

        return RedirectToAction("Index");

    }

I call the UpdateModel. All properties are filled in properly except for the Person.Parameters. This is always null after the updateModel.

Is there a solution for this or a workaround?

regards, Steve


回答1:


In your view:

"Person.Parameters.Parameter[" + index + "]" 

should be

"Person.Parameters[" + index + "].Parameter"


来源:https://stackoverflow.com/questions/642939/mvc-updatemodel-complextype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!