如何为我的模型创建 Html.EditorFor 以节省时间?

发布于 2024-11-30 01:55:05 字数 2698 浏览 2 评论 0原文

我目前只有 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:

enter image description here

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那片花海 2024-12-07 01:55:05

您可以将您的部分作为文件添加到 ~/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.

甜嗑 2024-12-07 01:55:05

将视图更改为如下:

@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>

这将输出模型的所有属性。关于其使用的精彩帖子此处

Change your view to as follows:

@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>

this will output all properties of the model. Great post on its use here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文