0

I've been trying to dissect an automated MVC view & controller, and modify it so that i can insert information from a form in the view, on to two separate database tables (Entity framework database first).

here's what i have so far. it works if i reference either of the namespaces at the top on their own, but not together?

@model manage.mysite.DataModels.Property_Type
@model manage.mysite.DataModels.Room_Type

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) 
{

<div class="form-horizontal">
    <h4>Property_Type</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.PropertyType, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PropertyType, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PropertyType, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.RoomType, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.RoomType, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.RoomType, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}
Gavin5511
  • 791
  • 5
  • 31

1 Answers1

3

It is not possible to reference several models in a View.

However, you can create composite model, which has the two other models as properties.

As such:

using manage.mysite.DataModels;

namespace manage.mysite.ViewModels
{ 
    public class FooViewModel
    {
        public Property_Type PropertyType { get; set; }
        public Room_Type RoomType { get; set; }
    }
}

And then serve this model instead.

@model manage.mysite.ViewModels.FooViewModel

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) 
{
    <div class="form-horizontal">
        <h4>Property_Type</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.PropertyType.PropertyType, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PropertyType.PropertyType, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PropertyType.PropertyType, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.RoomType.RoomType, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.RoomType.RoomType, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.RoomType.RoomType, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

See also:

MVC Multiple Models in One View

how to create a view with multiple models mvc 4?

Which is the best way to use multiple models with sames properties and using a unique view?

Community
  • 1
  • 1
mausworks
  • 1,607
  • 10
  • 23