0

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.

2 Answers2

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),
  • I dont know why but its not working for me. It still showing first item as selected –  Aug 19 '17 at 08:10
  • Then your doing something else wrong! Is this in an `EditorTemplate` where the model is a collection? –  Aug 19 '17 at 08:11
  • And you can take a look at this [DotNetFiddle](https://dotnetfiddle.net/OxriS9) which is associated with [this answer](https://dotnetfiddle.net/OxriS9) to prove it –  Aug 19 '17 at 08:13
  • 1
    Another possible cause is you have a parameter `string status` in your GET method (or a model with a property of that name) and its value is not "Issued"` –  Aug 19 '17 at 08:37
  • Changing `status` to new name worked :), Thanks –  Aug 19 '17 at 08:43
  • Then I assume you did have a parameter `string status` in the method? The value of the parameter is added to `ModelState` by the `DefaultModelBinder` and the `HtmlHelper` methods use values from `ModelState` if they exist (to understand the behavior, refer [this answer](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) –  Aug 19 '17 at 08:45
0

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"
adiga
  • 34,372
  • 9
  • 61
  • 83
  • I tried but it still showing the first item –  Aug 19 '17 at 08:09
  • i did have a parameter `string status` in the method, so changed the name to new and this code woks –  Aug 19 '17 at 08:50