Alireza Noori
Alireza Noori

Reputation: 15255

Create multiple parts of complex model in the main view

I'm a little new to ASP.Net MVC, I have a complex model.

public class BuildingPermit
{
    public int ApplicationID { get; set; }
    public virtual Person Applicant { get; set; }
    public virtual Area ApplicantArea { get; set; }
    public virtual ICollection<Owner> Owners { get; set; }
    /...
}

Using scaffolding, I created the controller and all the views. However, I want to register all the details in the same page, meaning in the BuildingPermit's Create view, creating the details for Applicant of type Person, the ApplicationArea of type Area and so on. Is there any way I can accomplish this?

If it's not possible, I think it's possible to add a link to create the object. When the user clicks on it, the page goes to that view, creates it, get its information back and shows it in the BuildingPermit's view.

I'd appreciate your help.

Upvotes: 1

Views: 99

Answers (2)

Macs Dickinson
Macs Dickinson

Reputation: 987

You could achieve this by creating an editor template for Person, Area, Owner etc in:

~/Views/Shared/EditorTemplates/Person.cshtml
~/Views/Shared/EditorTemplates/Area.cshtml
~/Views/Shared/EditorTemplates/Owner.cshtml

The editor template will want to be strongly typed and should give the editor layout for the type:

@model Models.Person
<h2>Person</h2> 
<p>
    @Html.LabelFor(model => model.Name)
    @Html.EditorFor(model => model.Name)
</p>
<p>
    @Html.LabelFor(model => model.Address)
    @Html.EditorFor(model => model.Address)
</p>
// And so on

Once you've done this calling @Html.EditorFor(model => model.Applicant) will pick up your template and display within your Edit view.

If you are wanting to display all of this information together then you will probably want to also create display templates for these types. These work just like the editor templates except you keep your templates in a DisplayTemplates folder.

~/Views/Shared/DisplayTemplates/Person.cshtml
~/Views/Shared/DisplayTemplates/Area.cshtml
~/Views/Shared/DisplayTemplates/Owner.cshtml

Upvotes: 1

Adrian Thompson Phillips
Adrian Thompson Phillips

Reputation: 7139

That's no problem, just make sure you initialise your complex object somehow to avoid null reference exceptions:

public BuildingPermit()
{
    this.Applicant = new Person();
    this.ApplicantArea = new Area();
    ...
}

Then in your controller action method create an instance of the model and pass it to your view:

public ActionResult Create()
{
    BuildingPermit model = new BuildingPermit();

    View(model);
}

For the view:

@model MyNamespace.BuildingPermit

@Html.LabelFor(m => m.Applicant.FirstName)<br />
@Html.TextBoxFor(m => m.Applicant.FirstName)<br />

...

<input type="submit" value="Create new building permit" />

Then look into examples online on how to handle a HttpPost in your MVC controller.

If you want to create specific UI partials for each object type, then you can looking into EditorFor and DisplayFor templates. From what you mention in your original post, this might be what you're looking for also.

Hope this helps.

Upvotes: 0

Related Questions