Result of LINQ Query in MVC SelectList as Value and Text - not working

醉酒当歌 提交于 2019-12-05 06:42:47

I thinks is not so nice to use ViewBag to populate dropdowns in mvc. You shoud change you model to have a list of SelectedListItem and populate it from list of PriceList so it should look like this :

public class Model
{
     public int CustomerID { get; set; }
     public int SelectedCustomer { get; set; }
     public IList<Pricelist> PriceList{get;set;}
     public IList<SelectListItem> PriceListSelectListItem{get;set;}
        {
            get
            {
                   var list = (from item in PriceList
                            select new SelectListItem()
                            {
                                Text = item.customerID.ToString(CultureInfo.InvariantCulture),
                                Value = item.selectedCustomer.ToString(CultureInfo.InvariantCulture)
                            }).ToList();
                return list;
            }
            set{}
        } 

}

and now in view you will be able to do something like this :

 @Html.DropDownListFor(c=>c.CustomerID,Model.PriceList)

Try to use dropdown like this, this will look more profesional than using ViewBag in views.

Controller:

var query = ((from s in db.Pricelists
                     select s.CustId).Distinct()).ToList();

int i = 1;
List<Pricelist> CustomerList = new List<Pricelist>();
foreach (var c in query)
{
    int cust = c;
    Pricelist p = new Pricelist(i, cust);
    CustomerList.Add(p);
    i++;
}

    model.PriceList=result of your query

return View("ViewName",model); 

The easiest method is to let Razor worry about the SelectList and just feed it an IEnumerable<SelectListItem>.

ViewBag.custList = CustomerList.Select(m => new SelectListItem { Value = m.Id, Text = m.Name });

Then in your view:

@Html.DropDownListFor(m => m.SomeField, (IEnumerable<SelectListItem>)ViewBag.custList)

You can make that nicer by using a view model that you can strongly type instead of ViewBag. You should really avoid ViewBag as much as possible.

You can change your dropdownlist in your view

@Html.DropDownList("custList", new SelectList(ViewBag.custList, "Id" , "Name"))

And your ViewBag in Controller (1) as

ViewBag.custList = CustomerList;

On your CustomerList object, set DataTextField and DataValueField properties.

Something like this:

SelectList CustomerList = new SelectList(query);
CustomerList.DataTextField = "Name"; // Name property of customer object
CustomerList.DataValueField = "Id"; // id property of customer object

ViewBag.custList = CustomerList;

I think this could help:

@Html.DropDownList("custList", (SelectList) ViewBag.custList)

Thanks for all your answers, I understand more about how SelectList works now, but don't have the level of skill to make all these answers work. This worked in the end:

Controller:

 public ActionResult Create()
    {
        var query = (from s in db.Pricelists
                     select s.CustId).Distinct();

        SelectList custList = new SelectList(query);


        ViewBag.custList = custList;

        return View();
    }

View:

    @Html.DropDownListFor(model => model.CustId  , (SelectList)ViewBag.custList, "--Select--")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!