开发者

Selecting the DropDownList Item Through ViewData

I am putting the current pageSize (5,10,15,20,All) in the ViewData inside the Controller. Now, I am trying to set the DropDownlist to set the selected value to the ViewData value.

  @Html.Dro开发者_StackOverflowpDownListFor(x => x.GridItems, new SelectList(Model.GridITems, "Value", "Text",ViewData["SelectedItemItemValue"].ToString()))

The above does not work and the first item is automatically selected when the page calls the action.


The first argument to DropDownListFor should be the item in the Model that you want the selected value to be bound to. For example, if you had a property on your model called 'SelectedItemValue' then your code should be as follows

 @Html.DropDownListFor(x => x.SelectedItemValue, new SelectList(Model.GridITems, "Value", "Text",ViewData["SelectedItemItemValue"].ToString()))

(the last argument in the select list constructor is for the selected value so this assumes that you have something with a key of SelectedItemItemValue in your ViewData

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜