0

Let's say I have two tables. Inventory and Supplier. Inventory has three fields : name, inventory_no and supplier_no and Supplier has two fields : name, supplier_no

In my View for Inventory is it possible to add a cell with a @Html.DropDownListFor() populated from a separate supplies model?

I can't figure out how to accomplish it using the Controller/View so I was thinking about using AJAX

Example:

Name    | Inv_no | Suppliers
------- | ------ | ------------------------------
Pen     | 101    | DDL populated of all suppliers
Pencil  | 102    | DDL populated of all suppliers
Erase   | 103    | DDL populated of all suppliers

VIEW:

<table>
    <tr>
        <th>Name</th>
        <th>Inventory_No</th>
        <th>Suppliers</th>
    <tr>
        @foreach(var item in Model)
        {
            <td>@Html.DisplayFor(modemItem => item.name)</td>
            <td>@Html.DisplayFor(modemItem => item.inventory_no)</td>
            <td><!-- @Html.DropDownListFor() ? --></td>
        }
</table>

Controller:

public virtual JsonResult  getListOfSuppliers(int? supplier_no)
{
    using (db)
    {
        var vendors = db.vendors.Select(c => new
        {
            Value = c.supplier_no,
            Text = c.name
        });
        return Json(vendors, JsonRequestBehavior.AllowGet);
    }
}
1
  • Yes, it is possible. Commented Jan 4, 2017 at 20:50

3 Answers 3

1

Yes, it is possible.

Your Model should be like:

    public class ViewModel
    {
        public string Name { get; set; }
        public int InventoryNo { get; set; }
        public int SupplierNo { get; set; }
        public IList<SelectListItem> Suppliers {get;set;}    
    }

Controller:

public virtual ActionResult  getListOfSuppliers(int? supplier_no)
{
    using (db)
    {
        var model = new ViewModel
        {
           Name = <name value>,
           InventoryNo = <name value>,
           Suppliers = db.vendors.Select(c => new SelectListItem
           {
              Value = c.supplier_no,
              Text = c.name
           }).ToList()
        };
        return View(model);
    }
}

in View:

<td><!-- @Html.DropDownListFor(x => x.SupplierNo, Model.Suppliers, new { @class = "form-control" }) ? --></td>
Sign up to request clarification or add additional context in comments.

Comments

0

Not really.

The easiest way would be to create a viewmodel and then use that model in the view. The viewmodel class would be generated by the controller and then passed to the view. In this viewmodel object would be the inventory items as well as an IEnumerable list of the supplies objects.

Something to this effect would get the ball rolling:

public class InventoryViewModel
{
    public string Name { get; set; }
    public int Inventory_No { get; set; }
    public int Supplier_No { get; set; }

    public List<Supplies>
    {
        get
            {
                 return <your DAL>.Supplies.All().ToList();
            }
    }
}

Comments

0

You don't need to use AJAX for that, just change the model you're passing into the view. The model would be List<Item>, and Item would be defined as:

string Name
int Inv_No
IEnumerable<SelectListItem> Suppliers

This way, each item has its own supplier list, and you can use DropDownFor to create the dropdown for each item from the Suppliers field.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.