我正在尝试制作一个自定义HtmlHelper,它将在现有HtmlHelpers(如Html.TextBoxFor
和Html.LabelFor
)的帮助下生成多个HTML元素。我正在运行的主要问题是我无法创建表达式以供这些现有的帮助器使用,除非它们全部单独传递到我的自定义HtmlHelper中。我希望能够将整个模型传递给自定义HtmlHelper,并在帮助器中创建表达式并将它们传递给现有的表达式。我试图这样做,希望这些属性仍然有效,例如DisplayName的Required和Internationalization。这可能吗?自定义MVC HtmlHelper使用现有HtmlHelpers的表达式
我知道我可以通过将每个Address属性传递给通过表达式的自定义HtmlHelper来实现,但是它不像使用辅助函数那样干净利落。
地址型号
public class Address
{
[Required(ErrorMessage = "Required"), DisplayName("First Name")]
public String FirstName { get; set; }
[Required(ErrorMessage = "Required"), DisplayName("Last Name")]
public String LastName { get; set; }
[Required(ErrorMessage = "Required"), DisplayName("Line 1")]
public String Line1 { get; set; }
[Required(ErrorMessage = "Required"), DisplayName("Line 2")]
public String Line2 { get; set; }
[Required(ErrorMessage = "Required"), DisplayName("City")]
public String City { get; set; }
[Required(ErrorMessage = "Required"), DisplayName("State")]
public String State { get; set; }
[Required(ErrorMessage = "Required"), DisplayName("Country")]
public String Country { get; set; }
[Required(ErrorMessage = "Required"), DisplayName("Postal Code")]
public String PostalCode { get; set; }
}
定制的HtmlHelper
public static class AddressExtension
{
public static MvcHtmlString AddressEditorForModel<TModel>(this HtmlHelper<TModel> helper, Address address, String prefix, Boolean showLabels)
{
// There will be more markup in here, this is just slimmed down..
StringBuilder sb = new StringBuilder();
sb.AppendLine(String.Format("<div id='{0}_AddressContainer' >", prefix));
// First Name
if (showLabels)
sb.AppendLine(helper.DisplayFor(????).ToString());
sb.AppendLine(helper.EditorFor(????).ToString());
// Last Name
if (showLabels)
sb.AppendLine(helper.DisplayFor(????).ToString());
sb.AppendLine(helper.EditorFor(????).ToString());
// Line 1
if (showLabels)
sb.AppendLine(helper.DisplayFor(????).ToString());
sb.AppendLine(helper.EditorFor(????).ToString());
// Line 2
if (showLabels)
sb.AppendLine(helper.DisplayFor(????).ToString());
sb.AppendLine(helper.EditorFor(????).ToString());
// City
if (showLabels)
sb.AppendLine(helper.DisplayFor(????).ToString());
sb.AppendLine(helper.EditorFor(????).ToString());
// State
if (showLabels)
sb.AppendLine(helper.DisplayFor(????).ToString());
sb.AppendLine(helper.EditorFor(????).ToString());
// Country
if (showLabels)
sb.AppendLine(helper.DisplayFor(????).ToString());
sb.AppendLine(helper.EditorFor(????).ToString());
// Postal Code
if (showLabels)
sb.AppendLine(helper.DisplayFor(????).ToString());
sb.AppendLine(helper.EditorFor(????).ToString());
sb.AppendLine("</div>");
return MvcHtmlString.Create(sb.ToString());
}
视图(CSHTML)
@Html.AddressEditorForModel(Model, "test", true)
这工作得很好,谢谢!我不太清楚TModel是如何工作的,所以谢谢你的解释。有没有办法只允许它与传入的地址一起使用,而不是通用的TModel? – WebDevNewbie
您可以在方法删除中放置一个泛型类型约束“where TModel:address”。这意味着你的方法只会在“Html”的intellisense中显示。当模型是Address时助手。有关泛型类型约束的更多信息,请参见此处: http://msdn.microsoft.com/en-gb/library/d5x73970.aspx – jcharlesworthuk
完美!非常感谢你的帮助:) – WebDevNewbie