坐落在Listboxfor默认选中的选项 [英] Set a Default selected option in Listboxfor
问题描述
我使用ListBoxfor来填充名称的列表。这是完全正常工作。我只想说ListboxFor $ P $默认pselects的最高值,当用户打开表单。
下面是我的code
(%)= Html.ListBoxFor(M = GT; m.Names,Model.Names,新{类=NamesControl})%GT;
我怎样才能让第一个选项所强调的默认选择?
在评论,我指着另一个答案。但是它改变了控制器默认选择的项目。有没有办法,我只能考虑改变?
下面是我的模型
公开名单< SelectListItem>名称{搞定;组; }
公共字符串名称{;组; }
在我看来,我已经下code
<%= Html.ListBoxFor(M = GT; m.Name,Model.Names,新{类=NamesControl})%GT;
尝试:
@ Html.ListBox(M => m.Names,(IEnumerable的< SelectListItem>)(新[] {新SelectListItem(){值=1,文本= ABC,选择=真}}))
编辑2:
@ Html.ListBoxFor(M = GT; m.Names,新的SelectList(新listName_With_Id()(),ID,姓名,3),新{ CLASS =NamesControl})
名称
与 ID = 3
将被默认选中
在这里。
I am using ListBoxfor to populate a list of names. It is fully working. I just want that ListboxFor preselects the top value by default when a user opens the form.
Here is my code
%= Html.ListBoxFor(m => m.Names, Model.Names, new { Class = "NamesControl"})%>
How can I make the first option highlighted selected by default?
In comments I was pointed to another answer. but it changes the controller to select the items by default. Is there a way I can only change in view?
Here is my Model
public List<SelectListItem> Names { get; set; }
public string Name { get; set; }
In my view I had following code
<%= Html.ListBoxFor(m => m.Name, Model.Names, new { Class = "NamesControl" })%>
try:
@Html.ListBox(m => m.Names, (IEnumerable<SelectListItem>)(new[] { new SelectListItem() { Value = "1",Text="Abc",Selected=true }}))
Edit 2:
@Html.ListBoxFor(m => m.Names, new SelectList(new listName_With_Id()(), "Id", "Name", "3"), new { Class = "NamesControl"})
Name
with Id =3
will be selected by default
here.
这篇关于坐落在Listboxfor默认选中的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!