MVC的UpdateModel的ComplexType [英] MVC UpdateModel ComplexType

查看:140
本文介绍了MVC的UpdateModel的ComplexType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有以下类型。

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

您让你的页面stronglytyped到人。

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

    }

我称之为的UpdateModel。所有属性都填写正确除外Person.Parameters。这始终是空的的UpdateModel后。

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

推荐答案

在您的视图:

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

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

这篇关于MVC的UpdateModel的ComplexType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆