MrTofu
MrTofu

Reputation: 3

Mvc 5 - 2 model in view

I just started with MVC and I'm trying to learn how to create an order and order details project.

I have also an Inventory which contains all the items in there but I am facing some problems trying to pull the data out from the inventory through the order details.

How do I combine this two together? @model InventoryTest.Models.Inventory.order and @model IEnumerable< InventoryTest.Models.Inventory.Inventories> in the view code?

I apologies for the messy structure of the code as I'm still learning but I do hope that someone could advice me on the problem I'm facing.

Inventory Model:

public int InventoryID { get; set; }
public string ItemNo { get; set; }
public string Item { get; set; }
public int Quantity { get; set; }

Order Model:

public int OrderID { get; set; }
public DateTime Date { get; set; }
public int EmployeeID { get; set; }
public int DepartmentID { get; set; }

 public IEnumerable<SelectListItem> GetEmployee()
{
    var query = db.Employees.Select(c => new SelectListItem
    {
        Value = c.EmployeeID.ToString(),
        Text = c.DisplayName,
    });
    return query.AsEnumerable();
}

 public IEnumerable<SelectListItem> GetDeptList()
{
    var query = db.Departments.Select(c => new SelectListItem
    {
        Value = c.DepartmentID.ToString(),
        Text = c.Description,
    });
    return query.AsEnumerable();
}

Order Detail Model:

public int OrderDetailID { get; set; }
public int Quantity { get; set; }
public int OrderID { get; set; }
public int InventoryID { get; set; }

On my view code for order create is as follows:

  @model InventoryTest.Models.Inventory.Order

@{
    ViewBag.Title = "Order Forms";
}

<h2>Order Forms</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()


    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="row">
            <div class="col-sm-4">
                @Html.LabelFor(model => model.EmployeeID, htmlAttributes: new { @class = "control-label col-md-2" })

                <div class="col-md-offset-4">
                    @Html.ValidationMessageFor(model => model.EmployeeID, "", new { @class = "text-danger" })

                    @Html.DropDownListFor(m => m.EmployeeID, Model.GetEmployee(), "Please Select", new
               {
                   @style = "width: 200px;height:35px",
                   @class = "input-select",
                   @data_bv_notempty = "true",
                   @data_bv_message = "Please select project."
               })
                </div>
            </div>
            <div class="col-sm-4">
                @Html.LabelFor(model => model.Department, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-offset-4">
                    @Html.ValidationMessageFor(model => model.DepartmentID, "", new { @class = "text-danger" })

                    @Html.DropDownListFor(m => m.DepartmentID, Model.GetDeptList(), "Please Select", new
               {
                   @style = "width: 200px;height:35px",
                   @class = "input-select",
                   @data_bv_notempty = "true",
                   @data_bv_message = "Please select project."
               })
                </div>
            </div>
        </div>
    </div>

        <hr />
}
<br />

<h4>Item Listing</h4>

<table class="table table-hover">
    <tr>
        <th>S/N</th>
        <th>Item No.</th>
        <th>Item</th>
        <th>Quantity</th>
    </tr>


*//I want to use foreach to populate the data here*
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>

</table>
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Upvotes: 0

Views: 157

Answers (2)

DavidS
DavidS

Reputation: 2934

Create a view model class:

public class OrderAndInventoryViewModel
{
    public Order OrderInfo { get; set; }
    public IEnumerable<Inventories> InventoryInfo { get; set; } 
}

And in your view, use:

@model <your namespace goes here>.OrderAndInventoryViewModel

Assign the appropriate order and inventory information into the OrderAndInventory object in your controller, and pass that to the view.

Upvotes: 1

Thermite
Thermite

Reputation: 1

One solution would be to create a new object, call it something like "OrderViewModel". That object could contain your Order and Inventory list. Then pass OrderViewModel to the View...

namespace InventoryTest.Models.Inventory {
    public class OrderViewModel {
        public Order order { get; set; }
        public IEnumerable<InventoryTest.Models.Inventory.Inventories> inventories { get; set; }
    }
}

Then the @model at the top of the View would be

@model InventoryTest.Models.Inventory.OrderViewModel

In the View, you can access the EmployeeID as...

Model.order.EmployeeID

Upvotes: 0

Related Questions