Only Bolivian Here
Only Bolivian Here

Reputation: 36733

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

Upvotes: 0

Views: 998

Answers (2)

stack72
stack72

Reputation: 8278

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

Upvotes: -1

marcind
marcind

Reputation: 53183

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.

Upvotes: 2

Related Questions