开发者

How do I create a Html.EditorFor for my model in order to save time?

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开发者_如何学Python.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:

How do I create a Html.EditorFor for my model in order to save time?

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


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:

@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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜