丢失局部视图模型表单值

时间:2017-08-29 19:57:24

标签: asp.net-mvc viewmodel partial-views

我有一个表单(使用主视图模型),然后是部分视图来实现表单的地址部分(secondary viewmodel)。当我提交表单时,我没有收回表单内部部分视图部分的值。没有任何错误,只是丢失部分的值。代码如下:

表格的“主要”部分:

@model REMS.ViewModels.AddComplexViewModel

@{
    ViewBag.Title = "Add Complex";
}

<h2>@ViewBag.Title</h2>

@{ Html.Partial("~/Views/Shared/Partials/ActionStatusPartial.cshtml", Model.ActionStatusMessageViewModel); }

@using (Html.BeginForm("AddComplex", "Admin", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>Add a New Complex</h4>
    <hr />    
    <div class="form-group">
        @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
        </div>
    </div>

    @Html.Partial("~/Views/Shared/Partials/AddressFormPartial.cshtml", Model.AddressViewModel)

    <div class="form-group">
        @Html.Label("Owner", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.DropDownList("SelectedOwner", Model.Owners, "Select an Owner...")
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Add" />
        </div>
    </div>
}

形式的部分视图:

@model REMS.ViewModels.AddressViewModel

<div class="form-group">
@Html.LabelFor(m => m.Address1, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
    @Html.TextBoxFor(m => m.Address1, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Address2, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Address2, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.City, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.City, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.State, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.State, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Zip, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Zip, new { @class = "form-control" })
    </div>
</div>

查看模特:

public class AddComplexViewModel
{
    [Display(Name = "Name")]
    public string Name { get; set; }

    [Display(Name = "AddressViewModel")]
    public AddressViewModel AddressViewModel { get; set; }

    [Display(Name = "SelectedOwner")]
    public Guid SelectedOwner { get; set; }

    [Display(Name = "Owners")]
    public SelectList Owners { get; set; }

    [Display(Name = "ActionStatusMessageViewModel")]
    public ActionStatusViewModel ActionStatusMessageViewModel { get; set; }

    public AddComplexViewModel()
    {
        ActionStatusMessageViewModel = new ActionStatusViewModel();
        AddressViewModel = new AddressViewModel();
    }
}


public class AddressViewModel
{
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
}

当我提交表单时,地址值都为空。我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

传递给部分主视图AddComplexViewModel模型以生成输入的正确名称。我假设在AddComplex方法中,您将AddComplexViewModel作为参数。

主要形式的变化:

@Html.Partial("~/Views/Shared/Partials/AddressFormPartial.cshtml", Model.AddressViewModel)

为:

@Html.Partial("~/Views/Shared/Partials/AddressFormPartial.cshtml", Model)

然后在部分视图中更改:

@model REMS.ViewModels.AddressViewModel

为:

@model REMS.ViewModels.AddComplexViewModel

和每个lambda一样:

m => m.Address1

为:

m => m.AddressViewModel.Address1

编辑后的整个局部视图:

@model WebApplication2.Controllers.AddComplexViewModel

<div class="form-group">
@Html.LabelFor(m => m.AddressViewModel.Address1, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
    @Html.TextBoxFor(m => m.AddressViewModel.Address1, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.AddressViewModel.Address2, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
    @Html.TextBoxFor(m => m.AddressViewModel.Address2, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.AddressViewModel.City, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
    @Html.TextBoxFor(m => m.AddressViewModel.City, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.AddressViewModel.State, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
    @Html.TextBoxFor(m => m.AddressViewModel.State, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.AddressViewModel.Zip, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
    @Html.TextBoxFor(m => m.AddressViewModel.Zip, new { @class = "form-control" })
</div>

答案 1 :(得分:0)

部分观点仍然是技术上的观点。您无法从局部获取值,因为它位于主视图的窗体内。

查看Editor Templates,它们可以让您实现所需的目标。

有关here

的更多信息