MVC 更新模型复杂类型

发布于 2024-07-14 13:45:41 字数 2984 浏览 9 评论 0原文

如果你有以下类型。

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

您将页面设置为 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> 

在控制器中如下:

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

    }

我调用 UpdateModel。 除 Person.Parameters 外,所有属性均已正确填写。 updateModel 之后它始终为 null。

是否有解决方案或解决方法?

问候, 史蒂夫

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2024-07-21 13:45:42

您认为:

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

应该是

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

In your view:

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

should be

"Person.Parameters[" + index + "].Parameter"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文