【问题标题】:MVC4 Partial view not loading values into "container" model on post backMVC4部分视图未在回发时将值加载到“容器”模型中
【发布时间】:2012-06-18 12:53:06
【问题描述】:

所以我想创建一个可重复使用的视图来编辑地址、电话号码等。

我设置了一个包含所有所需模型的容器模型。 创建了一个局部视图来处理 th 的地址部分 电子表格

但是当它回发到控制器时,主页面中存在客户数据,但部分视图中的任何内容都不存在(使用 MVC4/Razor)

容器模型

public class CustomerViewModel {
    public Customer CustomerData { get; set; }
    public Address MainAddress { get; set; }
    public Address ShippingAddress { get; set; }
    public Phone MainPhone { get; set; }
    public Phone Fax { get; set; }
}

控制器:

public ActionResult Edit(int id = 0) {
    CustomerViewModel model = new CustomerViewModel();
    model.CustomerData = Customer.FetchById(id);
    if (model.CustomerData == null) return HttpNotFound();

    //... load addresses, phones

    return View(model);
}

[HttpPost]
public ActionResult Edit(CustomerViewModel model) {
    if (ModelState.IsValid) {
        ///... save everything here - model has CustomerData, but nothing else
    }

    return View(model);
}

主视图:

@model ProjectName.WebSite.Models.CustomerViewModel

.....

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Customer</legend>
        @Html.HiddenFor(model => model.ModelCustomer.CustomerId)

        <div class="editor-label">
            @Html.LabelFor(model => model.ModelCustomer.CompanyName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ModelCustomer.CompanyName)
            @Html.ValidationMessageFor(model => model.ModelCustomer.CompanyName)
        </div>

        ...        

        @Html.Partial("Address", Model.MainAddress, new ViewDataDictionary {
            TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "Main" }
        })

        ...        

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}
.....

地址部分视图:

@model ProjectName.Business.Address

<fieldset style="margin-top:  20px;">
    <legend>@(ViewData["label"] ?? "Address")</legend>

    @Html.HiddenFor(model => model.AddressId)

    <div class="editor-label">
        @Html.LabelFor(model => model.Street)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Street)
        @Html.ValidationMessageFor(model => model.Street)
    </div>
    ...
</fieldset>

我在这里做错了什么 - 为什么我不能从部分视图中填充模型?

【问题讨论】:

  • 如果您自己回答了这个问题,请提供一个答案(下方)并接受它,以便将问题标记为已回答。
  • @Nayt,kingdango 是对的。请将您的问题中标有“已解决”的部分作为新答案移到下方。完成后,请将其标记为已接受的答案。这样做的一个很酷的结果是其他人可能会发现您的答案也很有用;如果他们这样做,他们会支持您的回答,这是参与此网站的有趣之处之一。
  • 我认为您最好在此处为 Adress 类型使用 EditorTemplate。这样可以避免您在以后维护代码时遇到很多麻烦。

标签: asp.net-mvc


【解决方案1】:

解决了!我想通了!睡不着,一不小心就睡着了!

在视图中,您必须确保 HtmlFieldPrefix 使用与复合模型类中相同的名称,因此由于我将两个地址命名为“MainAddress”和“ShippingAddress” - 只需确保相同的名称是设置分部时使用:

@Html.Partial("Address", Model.MainAddress, new ViewDataDictionary(Html.ViewDataContainer.ViewData) {
    TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "MainAddress" }
})

@Html.Partial("Address", Model.ShippingAddress, new ViewDataDictionary(Html.ViewDataContainer.ViewData) {
    TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "ShippingAddress" }
})

【讨论】:

  • 在我自己提出类似问题之前,我遇到了确切的问题并偶然发现了这篇文章。我正在做你所做的一切,除了在我的 @Html.Partial("Address", Model.ShippingAddress) 中我没有使用第三个 ViewDataDictionary 输入参数,并且在回发时遇到了同样的问题。为什么这个 ViewDataDictionary 部分是“必需的”——在我的情况下,我只有一种类型的地址,而不是像你这样的多种类型——所以在我的情况下说“Model.MainAddress”就足够了,我想? (是的,在包含第三个输入参数后一切正常)。
  • 即使您只有一个地址 - 第三个参数仍然告诉代码您的视图的哪个属性包含地址,在这种情况下,“MainAddress”是包含 AddressView 对象的视图的属性。 ..
猜你喜欢
  • 2017-01-24
  • 1970-01-01
  • 2016-11-26
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多