gpt4 book ai didi

asp.net-mvc - 嵌套的 EditorFor/DisplayFor 覆盖字段名称

转载 作者:行者123 更新时间:2023-12-03 15:02:50 24 4
gpt4 key购买 nike

没有覆盖 ViewData.TemplateInfo.HtmlFieldPrefix使用空字符串,有没有办法防止前缀通过一组嵌套的强类型 EditorFor 或 DisplayFor 帮助器调用?

这是我迄今为止工作的丑陋标记/代码:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Web.Mvc.SelectList>" %>
<% ViewData.TemplateInfo.HtmlFieldPrefix = ""; %>
<%=Html.DropDownList("sort", Model)%>

我试过 *For 允许指定 htmlFieldName 的重载。但这只会改变直接水平。如果我在调用时有前缀,我只需更改附加到前缀的内容。

我可以手动编写模板标记,但这样做是为了 SelectList对象似乎我最终会通过一次调整来复制 MVC 源,因为它涉及对象数据绑定(bind)逻辑。

最佳答案

如果您的 View 模型上有一个属性,而不是直接在模型上做下拉列表,您可以在其上放置一个 DataAnnontation 属性。

public class MyModel
{
[Display(Name="Your Favorite Choices")]
public string[] Choices {get; set;}
}

然后在你的代码中
<%= Html.LableFor(m => m.Choices) %><br />
<%=Html.DropDownList("sort", Model.Choices)%>

应该使用那个名字。

[希望我没记错。 :)]

这是 MSDN 链接:
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayattribute.displayattribute.aspx

关于asp.net-mvc - 嵌套的 EditorFor/DisplayFor 覆盖字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4168044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com