简体   繁体   English

使用下拉列表中的动态值设置属性

[英]set attribute using dynamic value from drop down list

Here i want to set a attribute "selected" in a dynamic drop down when it is clicked after HttpPost in MVC it shows the same first text "Please select a category". 在这里,我想在MVC中的HttpPost之后单击它时,在动态下拉列表中设置一个“选定”属性,它显示相同的第一个文本“请选择类别”。

@if (Model.objlist1.Count != 0) {
    <select id="catID" onchange="getID();this.form.submit();">
        <option id="removeIT" value="007">Please select a category</option>
        @foreach (var item in Model.objlist1) {
            //a++;
            <option value="@item.categoryID">@item.category</option>
        }
    </select>
    @Html.HiddenFor(m => m.categoryID, new { @id = "newCatID", @name = "newCatID" })
}

After reload from HttpPost i want to get the "selected" attribute in the dynamic drop down within foreach loop option tag by using the value to check and set attribute "selected" in option tag. 从HttpPost重新加载后,我想通过使用值来检查和设置选项标签中的“选定”属性,从而在foreach循环选项标签中的动态下拉列表中获取“选定”属性。

try like this In backend pass category 像这样尝试在后端通行证类别中

 ViewData["category"] =  new SelectList(
new List<SelectListItem>
{
    new SelectListItem { Text = "Cat1", Value = 1},
    new SelectListItem { Text = "Cat2", Value = 2},
}, "Value" , "Text");

in your view 在你看来

@Html.DropDownListFor(model => model.categoryID,  ViewData["category"] as IEnumerable<SelectListItem>, "Please select a category")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM