为什么 Html Helper 为视图模型的子项的属性添加前缀? [英] Why does Html Helper add prefix to attributes for child item of view model?

查看:30
本文介绍了为什么 Html Helper 为视图模型的子项的属性添加前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有单个子实体和各种其他属性的视图模型.

I have a view model with a single child entity and various other properties.

在我看来,我想为子实体显示一个表单.使用以下代码:

In my view I want to display a form for the child entity. Using the following code:

@Html.HiddenFor(model => model.Item.ItemID)

产生以下输出:

<input id="Item_ItemID" name="Item.ItemID" type="hidden" value="234" />

如您所见,Html 帮助程序在 idname 属性前加上了前缀,而我原以为输出是

As you can see, the Html helper has prefixed the id and name attributes, whereas I would have expected the output to be

<input id="ItemID" name="ItemID" type="hidden" value="234" />

因此,在提交表单时,前一个输出会导致错误,因为表单元素与子实体上的属性不对应.

Consequently, the former output causes an error when the form is submitted because the form elements do not correspond to properties on the child entity.

我知道我可以通过对隐藏字段进行硬编码来解决这个问题

I know I can get around this by hard-coding the hidden field

<input id="ItemID" name="ItemID" type="hidden" value="@Model.Item.ItemID" />

哪种打败了有Html助手的原因,或者创建一个局部视图并传入子对象

which kind of defeats the reason for having Html helpers, or creating a partial view and passing in the child object

@{Html.RenderPartial("ItemForm", Model.Item);}

我知道能够将 html 属性传递给该方法,并且还可以编写我自己的扩展方法,但是当涉及数据验证和 jQuery 时,事情变得更加复杂,即以下代码:

I am aware of being able to pass in html attributes to the method, and also writing my own extension method, but things get more complicated when data validation and jQuery are involved i.e the following code:

@Html.EditorFor(model => model.Item.Title)
@Html.ValidationMessageFor(model => model.Item.Title)

产生这个代码:

<input class="text-box single-line" data-val="true" data-val-required="The Title field is required." id="Item_Title" name="Item.Title" type="text" value="Some text" />
<span class="field-validation-valid" data-valmsg-for="Item.Title" data-valmsg-replace="true"></span>

因此需要一种优雅的方法来保持属性名称同步.

so there needs to be an elegant method to keep the property names in synch.

那么谁能回答为什么 HtmlHelper 为视图模型的子项的属性添加前缀?作为后续问题,是否还有其他巧妙的方法可以防止添加前缀?

So can anyone answer why does an HtmlHelper add a prefix to attributes for a child item of a view model? And as a follow up question, is there any other neat way of preventing the prefix from being added?

推荐答案

HTML 助手在构建时考虑了模型绑定约定.例如,我假设您有以下模型:

HTML helpers are built with model binding conventions in mind. So for example I suppose that you have the following model:

public class MyViewModel
{
    public ItemViewModel Item { get; set; }

    // ... other properties
}

public class ItemViewModel
{
    public int ItemID { get; set; }
}

并且相应的视图被强类型化为MyViewModel.

and the corresponding view is strongly typed to MyViewModel.

所以当你使用:

@Html.HiddenFor(model => model.Item.ItemID)

助手生成:

<input id="Item_ItemID" name="Item.ItemID" type="hidden" value="234" />

因为当表单被发送回服务器时,这将允许您在相应的控制器操作中正确绑定您的视图模型:

because that's what would allow you to properly bind your view model in the corresponding controller action when the form is POSTed back to the server:

[HttpPost]
public ActionResult Process(MyViewModel model)
{
    // model.Item.ItemID will be correctly bound here from the hidden field value
}

我猜您的问题是因为您的控制器操作将 ItemViewModel 作为参数而不是 MyViewModel:

I guess that your problems are coming from the fact that your controller action is taking an ItemViewModel as parameter instead of MyViewModel:

[HttpPost]
public ActionResult Process(ItemViewModel model)
{
    // model.ItemID will not be correctly bound here from the hidden field value
}

所以你可以使用 [Bind] 属性并指定一个前缀来帮助模型绑定器:

So you could use the [Bind] attribute and specify a prefix to help the model binder:

[HttpPost]
public ActionResult Process([Bind(Prefix="Item")] ItemViewModel model)
{
    // model.ItemID will be correctly bound here from the hidden field value
}

或者简单地设计另一个视图模型,从中去除所有不必要的属性,只留下 Item 属性.现在您的控制器操作可以采用这个专门设计的视图模型.

or simply design another view model stripping all the unnecessary properties from it and leaving only the Item property. Now your controller action could take this specifically designed view model.

正如你在一天结束时所看到的,如果你正确地设计你的视图模型,你可以完美地控制模型绑定.这都是关于 ASP.NET MVC 中的视图模型.他们解决了所有问题.

So as you can see at the end of the day you could perfectly fine control the model binding if you design your view models correctly. It's all about view models in ASP.NET MVC. They solve all problems.

这篇关于为什么 Html Helper 为视图模型的子项的属性添加前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆