如何持久化传递给局部视图的数据模型? [英] How to persist data models passed to partial views?

查看:23
本文介绍了如何持久化传递给局部视图的数据模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了说明我面临的问题,我将三个简单的数据模型放在一起:

 公共类 PersonalModel {公共字符串名字{获取;放;}公共字符串姓氏 { 获取;放;}}公共类地址模型{公共字符串街{得到;放;}}公共类整体模型{公共字符串 ID { 获取;放;}公共个人模型个人{得到;放;}公共地址模型地址{获取;放;}}

这是我的简单 Index.chtml:

@model WebAppTest.Models.OverallModel@using (Html.BeginForm()){@Html.AntiForgeryToken()<div class="form-horizo​​ntal"><h4>整体模型</h4><小时/>@Html.ValidationSummary(true, "", new { @class = "text-danger" })@Html.HiddenFor(model => model.Id)<div>@Html.Partial("Personal", Model.Personal)

<div>@Html.Partial("地址", Model.Address)

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

}

单击保存"按钮时,将调用以下控制器方法:

[HttpPost]公共 ActionResult 索引(整体模型模型){返回视图(模型);}

我遇到的问题是 model.Personalmodel.Address 值在控制器方法中总是显示为 null.

尽管值正确地传递给了局部视图,但在单击提交时,Razor 引擎无法将整个对象重新组合在一起.

如果您能启发我了解我遗漏了什么,我将不胜感激.问候.

解决方案

OverallModel 传递给您的部分,以便控件将正确命名以回发到 OverallModel.目前,您将拥有带有 name="FirstName" 的控件,但它们必须是 name="Personal.FirstName"

@Html.Partial("个人", 模型)

并更改部分以适应

@model 整体模型....@Html.TextBoxFor(m => m.Personal.FirstName)

作为替代,您也可以将前缀传递给部分

@Html.Partial("Address", Model.Personal, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "Personal" }})

To illustrate the problem I face, I have put together three simple data models:

 public class PersonalModel {
     public string FirstName { get; set; }
     public string LastName { get; set; }
 }

 public class AddressModel {
     public string Street { get; set; }
 }

 public class OverallModel {
    public string Id { get; set; }
    public PersonalModel Personal { get; set; }
    public AddressModel Address { get; set; }
 }

Here is my simple Index.chtml:

@model WebAppTest.Models.OverallModel
@using (Html.BeginForm())
{
  @Html.AntiForgeryToken()

  <div class="form-horizontal">
    <h4>OverallModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.Id)

    <div>
        @Html.Partial("Personal", Model.Personal)
    </div>
    <div>
        @Html.Partial("Address", Model.Address)
    </div>

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

When the "Save" button is clicked, the following controller method gets invoked:

[HttpPost]
public ActionResult Index(OverallModel model) {
    return View(model);
}

The problem I have is that model.Personal and model.Address values always show up as null in the controller method.

Although the values are correctly getting passed to the partial views, the Razor engine is not able to put the overall object back together when submit is clicked.

Would appreciate it if you could enlighten me on what is it that I am missing. Regards.

解决方案

Pass the OverallModel to your partials so that the controls will be correctly named for posting back to OverallModel. Currently you would have controls with name="FirstName" but they need to be name="Personal.FirstName"

@Html.Partial("Personal", Model)

and change the partials to suit

@model OverallModel
....
@Html.TextBoxFor(m => m.Personal.FirstName)

As an alternative, you can also pass the prefix to the partial

@Html.Partial("Address", Model.Personal, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "Personal" }})

这篇关于如何持久化传递给局部视图的数据模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆