控制器中未收到模型属性对象

时间:2017-03-09 13:12:32

标签: asp.net-mvc

我想访问控制器中Model“Address”的属性“Locality”。 如果我尝试赋值,则会给出错误:对象引用未设置为对象的实例。

型号:

public class Profile
{
    public string Id { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
    public DateTime DOB { get; set; }
}
public class Address
{
    public int FlatNumber { get; set; }
    public string Street { get; set; }
    public string Locality { get; set; }
}

控制器:

 public ActionResult Details(Profile pf)
    {
        pf.Address.Locality = "abc";
        return View();
    }

查看:

@model DownloadFileTest.Models.Profile
     <table>
                <tr>
                    <td style="font-weight:bold">ID</td>
                    <td>@Html.HiddenFor(Model => Model.Id)</td>
                </tr>
                <tr>
                    <td style="font-weight:bold">Name</td>
                    <td>@Html.TextBoxFor(Model => Model.Name, new { @class = "form-control fact", @disabled = "true" })</td>
                </tr>
                <tr>
                    <td style="font-weight:bold">Flat number( Large, Medium and Small)</td>
                    <td>@Html.TextAreaFor(Model => Model.Address.FlatNumber, new { htmlAttributes = new { @class = "form-control" }, rows = "3", cols = "50", @disabled = "true", @class = "fact" })</td>
                </tr>
                <tr>
                    <td style="font-weight:bold">Street</td>
                    <td>@Html.TextBoxFor(Model => Model.Address.Street, new { @class = "form-control fact", @disabled = "true" })</td>
                </tr>
            </table>

2 个答案:

答案 0 :(得分:1)

在您的视图中,您将所有Address属性标记为@disabled = "true",因此当您发布表单时 - 没有任何内容发布。这就是为什么MVC模型绑定器不会绑定任何内容而你在null类的Address属性中获得Profile的原因。

你可以这样解决:

 public ActionResult Details(Profile pf)
 {
     pf.Address = new Address();
     pf.Address.Locality = "abc";
     return View();
 }

甚至可以这样做:

public class Profile
{
    public Profile()
    {
        Address = new Address(); //here
    }

    public string Id { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
    public DateTime DOB { get; set; }
}

请注意,在绑定过程之前将调用无参数构造函数,因此如果您将在某一天发布POST地址值,它将正确绑定。

答案 1 :(得分:0)

您需要将模型传递给视图

public ActionResult Details(Profile pf)
{
    pf.Address.Locality = "abc";
    return View(pf);
}

要将数据恢复到控制器,您需要实现一个表单:

@using (Html.BeginForm("myActionName", "MyControllerName", null, FormMethod.Post, new { @class = "form-horizontal" }))
    {
        <table>
            <tr>
                <td style="font-weight:bold">ID</td>
                <td>@Html.HiddenFor(Model => Model.Id)</td>
            </tr>
            <tr>
                <td style="font-weight:bold">Name</td>
                <td>@Html.TextBoxFor(Model => Model.Name, new { @class = "form-control fact", @disabled = "true" })</td>
            </tr>
            <tr>
                <td style="font-weight:bold">Flat number( Large, Medium and Small)</td>
                <td>@Html.TextAreaFor(Model => Model.Address.FlatNumber, new { htmlAttributes = new { @class = "form-control" }, rows = "3", cols = "50", @disabled = "true", @class = "fact" })</td>
            </tr>
            <tr>
                <td style="font-weight:bold">Street</td>
                <td>@Html.TextBoxFor(Model => Model.Address.Street, new { @class = "form-control fact", @disabled = "true" })</td>
            </tr>
        </table>
        <input type="submit" class="btn btn-success btn-block" value="Submit" />
    }

在控制器中:

[HttpPost]
public ActionResult Details(Profile pf)
{
    // My Stuff
    return View();
}

Some reading on binding