'How to use a @Html.DropDownList with ViewBag?

I'm working with MVC4.

I've added the paging, sorting and filtering functions in an Index page.As a result, I have to add a viewbag parameter to the filtering DropdownList to keep the filtering string.

Here comes an example of a textbox with a viewbag parameter:

@Html.TextBox("name",ViewBag.CurrentFilterName as string) 

This works well.

I want to add a viewbag parameter to the DropDownList. Here comes my HTML code for this DropDownList:

@Html.DropDownList("type","All",ViewBag.CurrentFilterType as string).

This is wrong.

Could someone tell me how to correct it? Thank you very much!



Solution 1:[1]

conceivable that they can be: @Html.DropDownList("type", ViewBag.CurrentFilterType).

Check this: MVC @Html.DropDownList Getting Error with SelectList in ViewBag

Solution 2:[2]

try this code: View:

<td id="dropdown">
    @Html.DropDownList("Parameter", ViewBag.Parameter as SelectList)
</td>

controller:

public ActionResult Index()
{
    ViewBag.Parameter = new SelectList(_dataModel.GetWeightage(), "Parameter");
    return View();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Bosco