asp.net-mvc – MVC4部分视图没有将值加载到“容器”模型中
发布时间:2020-11-14 09:01:22  所属栏目:asp.Net  来源:互联网 
            导读:所以我想创建一个可重用的视图来编辑地址,电话号码等. 我设置了一个包含所有需要的模型的容器模型. 创建部分视图来处理地址部分 e形式 但是当它返回到控制器时,客户数据就在主页面,但部分视图中的任何内容都不存在(使用MVC4 / Razor) 集装箱模型 public class
                
                
                
            | 
                         所以我想创建一个可重用的视图来编辑地址,电话号码等. 我设置了一个包含所有需要的模型的容器模型. 但是当它返回到控制器时,客户数据就在主页面,但部分视图中的任何内容都不存在(使用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> 
 我在这里做错了什么 – 为什么我不能从部分视图中获取模型? 解决方法解决了!我想到了!睡不着,只是绊倒了!在视图中,您必须确保HtmlFieldPrefix使用与您的组合模型类中相同的名称,因此,由于我将两个地址命名为“MainAddress”和“ShippingAddress” – 只需确保在设置时使用相同的名称部分: @Html.Partial("Address",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" }
})                        (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
相关内容
推荐文章
            站长推荐
            - Jquery 组合form元素为json格式,asp.net反序列化
 - asp.net-mvc-3 – MVC 3不显眼的验证 – 有条件地
 - Asp.Net 5分钟实现网页实时监控
 - asp.net-mvc – MVC4部分视图没有将值加载到“容
 - asp.net-mvc – 在Asp.Net MVC中使用千位分隔符的
 - jQuery validate 根据 asp.net MVC的验证提取简单
 - 在ASP.NET中,什么决定了HostingEnvironment.IsDe
 - asp.net-mvc – 将viewdata传递给asp.net mvc ma
 - asp.net-mvc-4 – .net 4.5 ASP.Net web API JSO
 - ASP.NET(C#)应用程序配置文件app.config/web.con
 
热点阅读
            