0

I have the following tables PurchaseQuery, Suppliers. But PurchaseQuery can have more than 1 Supplier, so added a third table PurchaseQuerySupplier to keep both tables' IDs. enter image description here

I have PurchaseQuery form in which I added a multiselect list to select multiple Suppliers.

                            @Html.ListBoxFor(model => model.PurchaseQuerySuppliers, new MultiSelectList(Suppliers.AsEnumerable(), "ID", "Name"), new { @class = "chosen-select"})

But in my Action Controller I get null object of PurchaseQuerySuppliers. Although I can comma separated Supplier values in FormCollection, but I want to get PurchaseQuerySuppliers as an object inside PurchaseQuery in Action Controller. Any ideas?

2 Answers 2

1

You can write a view model that will solve the problem.

public class PurchaseSupplierViewModel
{
    public List<int> SelectedSupplies { get; set; } 
    public List<SelectListItem> Suppliers { get; set; } 
}

and your controller:

public ActionResult YourAction()
{
    List<Supplier> SupplierList = (get all Suppliers from DB here) 
    var model = new PurchaseSupplierViewModel()
    {
       Suppliers = SupplierList.Select(x => new SelectListItem
                        {
                            Value = x.ID.ToString(),
                            Text = x.Name,
                        }).ToList()
    };
    return View(model);
}

[HttpPost]
public ActionResult YourAction(PurchaseSupplierViewModel model)
{
    // model.SelectedSupplies will contain the selected Supplier IDs here
}

then the view:

@model PurchaseSupplierViewModel

@using (Html.BeginForm())
{
    @Html.ListBoxFor(x => x.SelectedSupplies, Model.Suppliers, new { @class = "chosen-select" })
    <input type="submit" value="Submit"/>
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here is how I would do it with some test examples:

Model:

    public class SuppliersModel
    {
        public List<Supplier> Suppliers { get; set; }
        public string[] PurchaseQuerySuppliers { get; set; }
    }

    public class Supplier
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

Actions:

    public ActionResult ListTest()
    {
        var model = new SuppliersModel();
        model.Suppliers = new List<Supplier>();
        model.Suppliers.Add(new Supplier { ID = 1, Name = "Name1"});
        model.Suppliers.Add(new Supplier { ID = 2, Name = "Name2" });
        return View(model);
    }

    [HttpPost]
    public ActionResult ListTest(SuppliersModel model)
    {
        string[] selectedItems = model.PurchaseQuerySuppliers;

        return View(model);
    }

View:

@model SuppliersModel
@using (Html.BeginForm())
{
@Html.ListBoxFor(model => model.PurchaseQuerySuppliers, new MultiSelectList(Model.Suppliers.AsEnumerable(), "ID", "Name"), new { @class = "chosen-select"})
<input type="submit" value="submit" />
}

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.