set select value in IEnumerable<SelectListItem> c# mvc [duplicate]

微笑、不失礼 提交于 2019-12-04 06:19:17

问题


I have a IEnumerable<SelectListItem>

My ViewModel

public class StatusClass
{
    public string Status { get; set; }

    public IEnumerable<SelectListItem> StatusList { get; set; }
}

I set values in to StatusList from my controller.

StatusClass statusObj = new CRM.StatusClass();

List<SelectListItem> Discountdata = new List<SelectListItem>();
        Discountdata.Add(new SelectListItem() { Value = "All", Text = "All" });
        Discountdata.Add(new SelectListItem() { Value = "Draft", Text = "Draft" });
        Discountdata.Add(new SelectListItem() { Value = "Issued", Text = "Issued" });
        Discountdata.Add(new SelectListItem() { Value = "Partial", Text = "Partially Received" });
        Discountdata.Add(new SelectListItem() { Value = "Received", Text = "Received" });
        Discountdata.Add(new SelectListItem() { Value = "PAID", Text = "Paid" });
        Discountdata.Add(new SelectListItem() { Value = "Billed", Text = "Billed" });

        statusObj.StatusList = new SelectList(Discountdata, "Value", "Text"); 

This works fine and my HTML is like this:

@Html.DropDownListFor(model => model.Status, Model.StatusList)

What i want is, I need to set the selected value from the controller when the list get created.

Suppose I have a string like this:

string newwStatus = "Issued";

How can I set it as selected in the SelectListItem.

I tried this, but its not working in my case:

foreach(var item  in StatusList)
{
    if(item.value == status)
    {
        item.Selected = true;
    }
}

and tries this too:

IEnumerable<SelectListItem> selectList =
    from s in StatusList
        select new SelectListItem
        {
            Selected = (s.Value == status),
            Text = s.Text,
            Value = s.Value
        };

I dont know if these are the right way if someone know how to do this, please help.

Thanks in advance.


回答1:


The Selected property of SelectListItem is ignored when binding to a model property. You need to set the value of your Status property in the GET method before you pass the model to the view

List<SelectListItem> Discountdata = new List<SelectListItem>
{
    new SelectListItem() { Value = "All", Text = "All" },
    new SelectListItem() { Value = "Draft", Text = "Draft" },
    new SelectListItem() { Value = "Issued", Text = "Issued" },
    ....

};
StatusClass model = new CRM.StatusClass
{
    StatusList = Discountdata,
    Status = "Issued"
};
return View(model);

and the 2nd option in your <select> element will be selected.

Note that Discountdata is already IEnumerable<SelectListItem> and using new SelectList(Discountdata, "Value", "Text") to create an identical IEnumerable<SelectListItem> is unnecessary extra overhead.

Note also that since you have the same value for both the value attribute and display text, you could simply use

List<string> Discountdata = new List<string>{ "All", "Draft", "Issued", ... };

and in the model constructor

StatusList = new SelectList(Discountdata),



回答2:


You're binding Status property to the dropdown. So set the value either from database, or in your case, a default value "Issued" like this:

statusObj.Status = "Issued"


来源:https://stackoverflow.com/questions/45769093/set-select-value-in-ienumerableselectlistitem-c-sharp-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!