如何为我的模型创建 Html.EditorFor 以节省时间?
我目前只有 Album 类的创建视图:
@model MvcApplication1.Models.AlbumViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>AlbumViewModel</legend>
<p>
@Html.LabelFor(model => model.GenreId)
@Html.DropDownListFor(x => x.GenreId, new SelectList(Model.Genres, "GenreId", "name"))
</p>
<p>
@Html.LabelFor(model => model.ArtistId)
@Html.DropDownListFor(x => x.ArtistId, new SelectList(Model.Artists, "ArtistId", "Name"))
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
我被告知,惯例是使用 EditorFor 进行创建和编辑,以便将来节省时间。
所以我的观点最终会是这样的:
@model MvcApplication1.Models.AlbumViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Html.EditorForModel
<div>
@Html.ActionLink("Back to List", "Index")
</div>
这是正确的吗?我如何以及在哪里声明并将其写在我的解决方案中?
编辑:
我在我的主帐户/注册视图中有这个:
@model Models.RegisterViewModel
@{
ViewBag.Title = "Register";
}
<h2>Register</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>RegisterViewModel</legend>
@Html.EditorFor(model => model.RegisterModel)
@Html.EditorFor(model => model.Cities)
@Html.EditorFor(model => model.Countries)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
在我的编辑器模板中:
@model Fooer.WebUI.Models.RegisterModel
THIS IS A TEST.
但它没有根本不渲染该虚拟文本。它呈现模型的默认控件。
I currently only have the create view for my Album class:
@model MvcApplication1.Models.AlbumViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>AlbumViewModel</legend>
<p>
@Html.LabelFor(model => model.GenreId)
@Html.DropDownListFor(x => x.GenreId, new SelectList(Model.Genres, "GenreId", "name"))
</p>
<p>
@Html.LabelFor(model => model.ArtistId)
@Html.DropDownListFor(x => x.ArtistId, new SelectList(Model.Artists, "ArtistId", "Name"))
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
I've been told that it's convention to use the EditorFor for both creating and editing in order to save time in the future.
So my view would end up looking like this:
@model MvcApplication1.Models.AlbumViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Html.EditorForModel
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Is this correct? How and where do I declare and write this up in my solution?
Edit:
I have this in my main Account/Register view:
@model Models.RegisterViewModel
@{
ViewBag.Title = "Register";
}
<h2>Register</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>RegisterViewModel</legend>
@Html.EditorFor(model => model.RegisterModel)
@Html.EditorFor(model => model.Cities)
@Html.EditorFor(model => model.Countries)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
And in my EditorTemplate:
@model Fooer.WebUI.Models.RegisterModel
THIS IS A TEST.
Yet it doesn't render that dummy text at all. It render the default controls for the model.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将您的部分作为文件添加到
~/Views/Shared/EditorTemplates/
下。Brad Wilson 关于 自定义对象模板拥有您需要的所有信息。
You would add your partial as a file under
~/Views/Shared/EditorTemplates/
.Brad Wilson's blog post on the subject of Custom object templates has all the information you need.
将视图更改为如下:
这将输出模型的所有属性。关于其使用的精彩帖子此处
Change your view to as follows:
this will output all properties of the model. Great post on its use here