Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top