andrea
andrea

Reputation: 381

CreateView and Controller for an Multiple Model with list .net mvc

i've got a entity made in this way,

public class UserTest : IdentityEntity
{
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Email { get; set; }
    public virtual string Phone { get; set; }
    public virtual string Fax { get; set; }
    public virtual DateTime BirthDate { get; set; }

    public virtual IList<UserAddress> Address { get; protected set; }
}

public class UserAddress : IdentityEntity
{
    public virtual string Address { get; set; }
    public virtual string City { get; set; }
    public virtual string Province { get; set; }
}

Every time i associated one Create View with one Model, but this time (and i wonder a lot of time's next) i will need to made a Create View with more than one Entity like this case.

Using Scaffolding Tool, VS doesn't prepare view for this List. How to proceed?

Upvotes: 0

Views: 119

Answers (1)

SBirthare
SBirthare

Reputation: 5137

We have something similar in our application where we allow admin to create new user. Models are similar to what you have. To render a list of addresses in Create User view, we have used Display and Editor templates.

These templates are similar to partial view and stored in Shared folder under "DisplayTemplates" or "EditorTemplates" folder.

Shared\DisplayTemplates\AddressViewModel.cshtml

@model Eda.RDBI.Web.Models.AddressViewModel

<h3>@Model.BillingType.ToString() Address</h3>

@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.OrganizationId)
@Html.HiddenFor(model => model.BillingType)


<div class="editor-label">
    @Html.LabelFor(model => model.ContactFirstName)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.ContactFirstName)
    @Html.ValidationMessageFor(model => model.ContactFirstName)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.ContactLastName)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.ContactLastName)
    @Html.ValidationMessageFor(model => model.ContactLastName)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Street1)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Street1)
    @Html.ValidationMessageFor(model => model.Street1)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Street2)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Street2)
    @Html.ValidationMessageFor(model => model.Street2)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.City)
    @Html.ValidationMessageFor(model => model.City)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.State)
    @Html.ValidationMessageFor(model => model.State)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Country)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Country)
    @Html.ValidationMessageFor(model => model.Country)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Zip)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Zip)
    @Html.ValidationMessageFor(model => model.Zip)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Email)
    @Html.ValidationMessageFor(model => model.Email)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.PhoneNumber)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.PhoneNumber)
    @Html.ValidationMessageFor(model => model.PhoneNumber)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Extension)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Extension)
    @Html.ValidationMessageFor(model => model.Extension)
</div>

Shared\EditorTemplates\AddressViewModel.cshtml

It has similar elements but uses EditorFor. I am not pasting it here as it is a big code.

Once you have templates in right folder, in your main view e.g. User\Create.cshtml, along with other html elements like userName, age, etc. have below line:

        <div class="row">
            @Html.EditorFor(m => m.Addresses)
        </div>

You can find example of how to use Display or Editor template in mvc online for example here. Also see this link for more help on templates. .

Hope this helps.

Upvotes: 1

Related Questions