7

I have multiple dropdown list for same select list in look and want to set dropdown item selected as per loop.

How can I set specific one item of dropdown list selected in mvc dropdownlist.

Please help.

0

4 Answers 4

9

The Html.DropDownList method takes multiple parameters, one of them being a List<SelectListItem>. The individual instance of the SelectListItem is where you set the Selected property:

var item = new SelectListItem() {

    Selected = /* condition */,

    Value = "Some Value",
    Text = "Some Text"
};

Alternatively:

Create a SelectList collection that exposes the SelectedValue property:

Model.YourSelectList = new SelectList(items /* List<SelectListItem> */,
                                      "Value",
                                      "Text",
                                      37 /* selected value */);
Sign up to request clarification or add additional context in comments.

4 Comments

@{ ViewData["Sectors"] = new SelectList(Model.SectorList, "MainCategoryId", "CatName", "37"); } @Html.DropDownList("SectorId" + i, (IEnumerable<SelectListItem>)ViewData["Sectors"], "-- Select --", new { @class = "inputbox-small" })
And?! I hope you didn't set them all to true. That would defeat the purpose of setting the Selected property ...
No, my value for each dropdown will be different which is "37" so Select List will be same but selected will be different in loop. Please correct me if I am wrong in my code.
Yes, I am constructing it. Any other way where I can easily set selected like optional label parameter?
2

When building the SelectList, you can set the selected item on construction using http://msdn.microsoft.com/en-us/library/dd460123.aspx

Or you can set it on an individual SelectListItem via it's Selected property ( http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem.selected.aspx ) and use the single-parameter constructor of the select list, or pass it straight to the DropDownList method.

Comments

2

Use the HTML helper ListBoxFor.

@Html.ListBoxFor(m => m.MyPropertyId, Model.MySelectList)

To build the list of items, you can use the MultiSelectList. For example, in your controller:

public ActionResult Index()
{
    // Get a collection of all product id's that should be selected.
    int[] productIds = _service.GetSomeProductIds();

    // Make a new select list with multiple selected items.
    ViewBag.List = new MultiSelectList(
        _service.Products, 
        "Id",                   // Name of the value field
        "Name",                 // Name of the display text field
         productIds );          // list of selected product ids

    return View();
}

Then in your view:

@Html.ListBoxFor(m => m.ProductIds, (IEnumerable<SelectListItem>)ViewBag.List)

Comments

0

MVC method to bind custom list to dropdownlist and select item dynamically if you need more details ,comment below

Create Section

@{
   List<SelectListItem> list = new List<SelectListItem>();
   list.Add(new SelectListItem { Text = "SALE", Value = "SAL" });
   list.Add(new SelectListItem { Text = "PURCHASE", Value = "PUR" });
}

<div class="form-group">
    @Html.LabelFor(model => model.SaleOrPurchase, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">           
        @Html.DropDownListFor(model => model.SaleOrPurchase, list, "-- Select Status --", new {@class= "form-control" })
        @Html.ValidationMessageFor(model => model.SaleOrPurchase, "", new { @class = "text-danger" })
    </div>
</div>

EDIT Section

     List<SelectListItem> list = new List<SelectListItem>();
     list.Add(new SelectListItem { Text = "SALE", Value = "SAL" });
     list.Add(new SelectListItem { Text = "PURCHASE", Value = "PUR" });

     IEnumerable<SelectListItem> myCollection = list.AsEnumerable();

     ViewBag.SaleOrPurchase = new SelectList(myCollection, "Value", "Text", transactionTbl.SaleOrPurchase.ToString().Trim());

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.