[英]DropDownList SelectedItem Not Working (Razor ASP MVC5)
Model 模型
public class MyModel {
public string ItemXYZ { get; set; }
}
Controller 调节器
public ActionResult Edit(int? id)
{
var x = db.XYZs.Find(id);
ViewBag.Item_XYZ = new SelectList(new[] { "X", "Y", "Z"}, x.CurrentXYZ);
}
View 视图
@Html.DropDownList("XYZ123", @ViewBag.Item_XYZ as SelectList, new { @class = "special-class" })
Problem 问题
If I change my DropDownList to mach the name of the get set in my model, the selected value does not work. 如果将我的DropDownList更改为模型中的get集合名称,则所选值不起作用。
@Html.DropDownList("ItemXYZ" ....)
If it matches the name of my ViewBag
item, the SelectedItem
doesn't work. 如果它与我的ViewBag
项目的名称匹配,则SelectedItem
不起作用。
@Html.DropDownList("Item_XYZ" ....)
But, if I append 123
(for example), SelectedItem
works just fine. 但是,如果我追加123
(例如), SelectedItem
效果很好。
UPDATE UPDATE
This is the same issue I am having, but I don't understand why or how to handle the return in my controller? 这是我遇到的相同问题 ,但是我不明白为什么或如何处理控制器中的收益?
In Controller use the code as:: 在Controller中,将代码用作:
public ActionResult Edit(int? id)
{
var x = db.XYZs.ToList();
ViewBag.TestDropdown = new SelectList(x, "ValueField", "TextField", "DefaultSelected");
}
and in Client side to show Dropdown use this: 并在客户端显示下拉菜单中使用以下命令:
@Html.DropDownList("TestDropdown")
thats it. 而已。 May be this will help you. 可能会对您有帮助。
Receive a parameter named XYZ123 in respective action. 在相应的操作中接收一个名为XYZ123的参数。 Also include XYZ123 in Bind attribute of action. 在动作的“绑定”属性中还包括XYZ123。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.