Jivenlans Tabien Jivenlans Tabien - 4 months ago 16
C# Question

how can I use viewbag without affecting the name of dropdown

I want to use a viewbag but I don't want to affect the name of the dropdown. I want a different name to use in dropdown. is it possible or It has another solution.

My Codes in View:

ViewBag.concurring1 = TempData["pathologistlist"];
@Html.DropDownList("concurring", "Select Pathologist")


There is another overload of DropDownList helper method which allows you to generate the SELECT element with custom name and id value

public static MvcHtmlString DropDownList(
    this HtmlHelper htmlHelper,
    string name,
    IEnumerable<SelectListItem> selectList,
    IDictionary<string, object> htmlAttributes

So your code will be

@Html.DropDownList("MyCustomName",ViewBag.concurring as List<SelectListItem>,"Select one")  

Assuming there is a ViewBag entry exist for concurring which is of type IEnumerable<SelectListItem>

var list= new List<SelectListItem>{
   new SelectListItem { Value="MI", Text="Michigan" },
   new SelectListItem { Value="NY", Text="New York" }
ViewBag.concurring = list;

The above code will generate the SELECT element with name id value set to "MycustomName"