简体   繁体   English

ASP.NET MVC ViewModel属性为null

[英]ASP.NET MVC ViewModel Property is null

I have the following ViewModel: 我有以下ViewModel:

public class ProjectViewModel
{
    public Project Project { get; set; }
    public Customer Customer { get; set; }
}

The Customer property is only used to link a new Project to the Customer, so I don't include this property in my Create view, which looks like this: Customer属性仅用于将新Project链接到Customer,因此我不会在“创建”视图中包括此属性,如下所示:

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Project</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Project.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Project.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Project.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

When I post the form, the following method in my ProjectsController is triggered: 发布表单时,将在ProjectsController中触发以下方法:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Project,Customer")] ProjectViewModel vm)
{
    var project = Mapper.Map<Project>(vm);
    if (ModelState.IsValid)
    {
        _db.Create(project);
        return RedirectToAction("Index");
    }
    return View(vm);
}

This is where the unexpected behaviour occurs. 这是发生意外行为的地方。 Now, when I examine the vm property, the Customer property is null . 现在,当我检查vm属性时, Customer属性为null

The question How can I still keep the Customer property filled, while not using it in the view? 问题:如何在视图中不使用“ Customer属性时仍将其填充?

如果要保留客户数据,则需要将所有字段设置为隐藏元素,否则它们将在重定向中丢失。

@Html.HiddenFor(model => model.Customer.Property1) ...etc...

The short answer is that you can't. 简短的答案是您不能这样做。 When data is posted back, the only data that is included is what is in the form on the HTML page. 当数据回发时,唯一包含的数据就是HTML页面上的表单中的数据。

Your best bet is to either use a session variable or look up the data again in the post handler, or alternatively, serialize the data to hidden fields. 最好的选择是使用会话变量或在后处理程序中再次查找数据,或者将数据序列化为隐藏字段。

To make sure the Customer property is never null when initializing ProjectViewModel , you could add a constructor to your ProjectViewModel class initializing the Customer property to a new Customer() like so: 为了确保在初始化ProjectViewModelCustomer属性永远不会为null ,您可以在ProjectViewModel类中添加一个构造函数,将Customer属性初始化为new Customer()如下所示:

public class ProjectViewModel
{
    // Constructor
    public ProjectViewModel()
    {
        Customer = new Customer();
    }

    public Customer Customer { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM