DropDownList的 - 没有PostValue了selectedValue未选择 [英] DropDownList - selectedValue without PostValue is not selected
问题描述
我使用asp.net MVC 2和我有与DropDownListFor帮手的问题。
我的视图模型包含UND 1的SelectedValue所有需要一个价值观的SelectList。
当我在查看使用DropDownListFor助手未选中的SelectedValue!
然后,我选择其他值并提交表单,就在PostedValue接下来的渲染选择。在第一次渲染的问题是什么?
<%= Html.LabelFor(M = GT; m.Test)%GT;
<%= Html.DropDownListFor(M = GT; m.Test,Model.TestList,新{的tabindex = 1,@class =选择框})%GT;
<%= Html.ValidationMessageFor(M = GT; m.Test,空,新的{@class =TEXT-隐藏,ID =错误试验})%GT;
从你提供了什么是不可能的说,为什么你还没有表现出既没有控制器,也没有视图模型这是行不通的。
下面是工作的例子:
型号:
公共类为MyModel
{
公共字符串测试{搞定;组; } 公共IEnumerable的< SelectListItem> TestList
{
得到
{
返回新的SelectList(新[]
{
新SelectListItem {值=1,文本=TEXT 1},
新SelectListItem {值=2,文本=TEXT 2},
新SelectListItem {值=3,文本=TEXT 3},
},值,文本,测试);
}
}
}
控制器:
公共类HomeController的:控制器
{
公众的ActionResult指数()
{
返回查看(新为MyModel {测试=2});
}
}
查看:
<%:Html.DropDownListFor(X => x.Test,Model.TestList)%GT;
i use asp.net mvc 2 and i have a problem with the DropDownListFor helper. My ViewModel contains a SelectList with all required Values und 1 SelectedValue. When i use the DropDownListFor helper in my View the SelectedValue is not selected! Then i select a other Value and submit the form, on the next rendering in the PostedValue selected. What is the problem on the first Rendering?
<%=Html.LabelFor(m => m.Test)%>
<%=Html.DropDownListFor(m => m.Test, Model.TestList, new { tabindex = 1, @class = "selectbox" })%>
<%=Html.ValidationMessageFor(m => m.Test, null, new {@class = "text-hide", id = "Error-Test"})%>
From what you've provided it is impossible to say why it doesn't work as you haven't shown neither the controller, nor the view model.
Here's an example that works:
Model:
public class MyModel
{
public string Test { get; set; }
public IEnumerable<SelectListItem> TestList
{
get
{
return new SelectList(new[]
{
new SelectListItem { Value = "1", Text = "text 1" },
new SelectListItem { Value = "2", Text = "text 2" },
new SelectListItem { Value = "3", Text = "text 3" },
}, "Value", "Text", Test);
}
}
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyModel { Test = "2" });
}
}
View:
<%: Html.DropDownListFor(x => x.Test, Model.TestList) %>
这篇关于DropDownList的 - 没有PostValue了selectedValue未选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!