【发布时间】:2012-11-04 08:01:42
【问题描述】:
我一直在试验 MVC(昨天开始),想知道是否可以创建我的“自定义控件”。
我有类似以下内容:
在我看来
@Html.EditorFor(model => model.UserName, "StringTemplate", new {LabelText = "Test" })
用于渲染控件的“自定义视图”
@model MvcApplication.Models.StringTemplate
@Html.LabelFor(model => model.Field, ViewData["LabelText"].ToString())
@Html.TextBoxFor(model => model.Field)
视图模型
public class RegisterModel
{
[Required]
public StringTemplate UserName { get; set; }
}
public class StringTemplate
{
[Required]
public string Field { get; set; }
public StringTemplate()
{
Field = String.Empty;
}
public StringTemplate(string field)
{
Field = field;
}
}
我想要做的是通过调用@Html.EditorFor,利用StringTemplate 并传递值LabelText。如何从视图“StringTemplate”中读取值“Test”?另外,有没有办法可以在“StringTemplate”视图中执行@Html.ValidationMessageFor()?
如果有任何教程或链接可以帮助我解决这个问题,我将不胜感激......
谢谢!
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 razor html-helper