另一个模型绑定问题

发布于 2024-12-22 15:10:18 字数 2013 浏览 1 评论 0原文

我昨天问过类似的问题 这里稍作修改。

这是我的模型

public class InfoModel    
{  
    public NameModel Name { get; set; }  
    public string Phone { get; set; }  
}  

public class NameModel
{
    public string Title     { get; set; }
    public string FirstName { get; set; }
    public string LastName  { get; set; }
    public List<SelectListItem> Titles 
    {
        get
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "Mr.", Value = "Mr." });
            list.Add(new SelectListItem() { Text = "Mrs.", Value = "Mrs." });
            list.Add(new SelectListItem() { Text = "Ms.", Value = "Ms." });

            return list;
        }
    }

    public NameModel()
    {

    }

    public NameModel(string first, string last) 
    {
        this.FirstName = first;
        this.LastName = last;
    }
}   

然后我有我的第一个视图 ShowName.cshtml 如下所示

@model MyTestApp.Models.NameModel

@Html.DropDownListFor(m => m.Title, Model.Titles, Model.Titles)
<br />
@Html.LabelFor( m => m.LastName) 
@Html.TextBoxFor( m => m.LastName)       
<br />
@Html.LabelFor( m => m.FirstName) 
@Html.TextBoxFor( m => m.FirstName)       

上面的视图在 ShowInfo.cshtml 中使用如下

@model MyTestApp.Models.InfoModel

@using (Html.BeginForm())
{
    @Html.Partial("ShowName", Model.Name)
    <br />
    @Html.LabelFor(m => m.Phone) 
    @Html.TextBoxFor(m => m.Phone)
    <br />
   <input type="submit" value="Submit Info" />
}

当用户提交任何信息时,将调用以下控制器方法

[HttpPost]
 public ActionResult ShowInfo(InfoModel model)
 {
    ...
 }

问题是电话很好,但名称为空。如果我将 @Html.Partial("ShowName", Model.Name) 的调用更改为 @Html.EditorFor(m => m.Name, "ShowName") 然后标题的下拉列表显示为编辑框

Its similar question that I asked yesterday here with a slight modification.

Here are my models

public class InfoModel    
{  
    public NameModel Name { get; set; }  
    public string Phone { get; set; }  
}  

public class NameModel
{
    public string Title     { get; set; }
    public string FirstName { get; set; }
    public string LastName  { get; set; }
    public List<SelectListItem> Titles 
    {
        get
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "Mr.", Value = "Mr." });
            list.Add(new SelectListItem() { Text = "Mrs.", Value = "Mrs." });
            list.Add(new SelectListItem() { Text = "Ms.", Value = "Ms." });

            return list;
        }
    }

    public NameModel()
    {

    }

    public NameModel(string first, string last) 
    {
        this.FirstName = first;
        this.LastName = last;
    }
}   

Then I have my first view ShowName.cshtml as follows

@model MyTestApp.Models.NameModel

@Html.DropDownListFor(m => m.Title, Model.Titles, Model.Titles)
<br />
@Html.LabelFor( m => m.LastName) 
@Html.TextBoxFor( m => m.LastName)       
<br />
@Html.LabelFor( m => m.FirstName) 
@Html.TextBoxFor( m => m.FirstName)       

the above view is used in ShowInfo.cshtml as below

@model MyTestApp.Models.InfoModel

@using (Html.BeginForm())
{
    @Html.Partial("ShowName", Model.Name)
    <br />
    @Html.LabelFor(m => m.Phone) 
    @Html.TextBoxFor(m => m.Phone)
    <br />
   <input type="submit" value="Submit Info" />
}

When User submit any info, follwoing controller method is called

[HttpPost]
 public ActionResult ShowInfo(InfoModel model)
 {
    ...
 }

The problem is that phone is fine but name is null. If I change my call of @Html.Partial("ShowName", Model.Name) to @Html.EditorFor(m => m.Name, "ShowName")
then the drop-down for Titles is displayed as an Editbox

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋千易 2024-12-29 15:10:18

我建议您使用编辑器模板。因此,不要使用 Html.Partial,而是使用:

@Html.EditorFor(x => x.Name, "ShowName")

,然后将部分移动到 ~/Views/Shared/EditorTemplates/ShowName.cshtml

这就是部分不起作用的原因:它为输入字段生成错误的名称。因此,当您编写:

@Html.TextBoxFor(m => m.LastName)

这会生成以下标记:

<input type="text" name="LastName" id="LastName" value="" />

而您需要:

<input type="text" name="Name.LastName" id="Name_LastName" value="" />

为了使默认模型绑定器正常工作并在主视图模型上分配 Name 属性的 LastName 属性。

当您使用编辑器模板时,如我的示例所示,它们会自动处理关联路径并为输入元素生成正确的名称。

I would recommend you using editor templates. So instead of the Html.Partial use:

@Html.EditorFor(x => x.Name, "ShowName")

and then move the partial to ~/Views/Shared/EditorTemplates/ShowName.cshtml.

Here's the reason why the partial doesn't work: it generates wrong names for the input fields. So when you write:

@Html.TextBoxFor(m => m.LastName)

this generates the following markup:

<input type="text" name="LastName" id="LastName" value="" />

whereas you need:

<input type="text" name="Name.LastName" id="Name_LastName" value="" />

for the default model binder to work correctly and assign the LastName property of the Name property on your main view model.

When you use editor templates, as shown in my example, they automatically take care of the association paths and generate proper names for the input elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文