Html.DropDownList (listname, list) does not return a list with the selected item

I recently added some partial views to be useful in another part of my code, and in doing so, I converted the DropDownList () calls to be able to use both the name and the list, rather than just using the list name:

MvcHtmlString s;
MvcHtmlString s2;
var list = ViewBag.AccountCodeList as SelectList;
s = Html.DropDownList("AccountCodeList"); //old style
s2 = Html.DropDownList("AccountCodeList", list); // new style 

However, the output of the two calls does not match (as I expected, they will be). The first call returns a string:

<select id="AccountCodeList" name="AccountCodeList">
    <option selected="selected" value="0">--None--</option>
    <option value="23">01L00000021150000000</option>
    <option value="40">10A00000071100000000</option>
    <option value="39">10R30129216000000000</option>
    <option value="2">2005-00-00</option>
    <option value="4">3105-00-00</option>
    <option value="5">4105-40-000</option>
    <option value="3">4250-40-12</option>
</select>

But the second returns this:

<select id="AccountCodeList" name="AccountCodeList">
    <option value="0">--None--</option>
    <option value="23">01L00000021150000000</option>
    <option value="40">10A00000071100000000</option>
    <option value="39">10R30129216000000000</option>
    <option value="2">2005-00-00</option>
    <option value="4">3105-00-00</option>
    <option value="5">4105-40-000</option>
    <option value="3">4250-40-12</option>
</select>

As you can see, the lists are identical ... except that the "new" style does not generate the "selected" attribute. I checked that listit actually contains SelectListItemthat has the "selected" property.

What am I missing? I seem to be unable to understand the reason for this unexpected (to me) behavior.

( ASP.NET MVC, ), , , .

s2:

 s2 = Html.DropDownList("test", list);

, !!

?

EDIT 2

, , , .
ASP.NET MVC Html.DropDownList SelectedValue , , , , .

3

, -, "": qaru.site/questions/59497/...

+4

Source: https://habr.com/ru/post/1524524/


All Articles