虽然不是[必需]列表字段按要求显示,但模型状态由于为空而无效?
我有以下代码-
视图-
<% Html.BeginForm(); %>
<div>
<%= Html.DropDownList("DropDownSelectList", new SelectList( Model.DropDownSelectList, "Value", "Text"))%>
控制器-
public ActionResult Admin(string apiKey, string userId)
{
ChallengesAdminViewModel vm = new ChallengesAdminViewModel();
vm.ApiKey = apiKey;
vm.UserId = userId;
vm.DropDownSelectList = new List<SelectListItem>();
vm.DropDownSelectList.Add(listItem1);
vm.DropDownSelectList.Add(listItem2);
vm.DropDownSelectList.Add(listItem3);
vm.DropDownSelectList.Add(listItem4);
vm.DropDownSelectList.Add(listItem5);
vm.DropDownSelectList.Add(listItem6);
vm.DropDownSelectList.Add(listItem7);
}
[HttpPost]
public ActionResult Admin(ChallengesAdminViewModel vm)
{
if (ModelState.IsValid)//Due to the null dropdownlist gives model state invalid
{
}
}
视图模型-
public class ChallengesAdminViewModel
{
[Required]
public string ApiKey { get; set; }
[Required]
public string UserId { get; set; }
public List<SelectListItem> DropDownSelectList { get; set; }
}
我不知道为什么它仍然需要列表,尽管不是必需的。我只想根据需要拥有两个属性。所以我想知道如何声明或更改该列表为不需要的并使我的模型状态有效。
I have the following code-
View-
<% Html.BeginForm(); %>
<div>
<%= Html.DropDownList("DropDownSelectList", new SelectList( Model.DropDownSelectList, "Value", "Text"))%>
Controller-
public ActionResult Admin(string apiKey, string userId)
{
ChallengesAdminViewModel vm = new ChallengesAdminViewModel();
vm.ApiKey = apiKey;
vm.UserId = userId;
vm.DropDownSelectList = new List<SelectListItem>();
vm.DropDownSelectList.Add(listItem1);
vm.DropDownSelectList.Add(listItem2);
vm.DropDownSelectList.Add(listItem3);
vm.DropDownSelectList.Add(listItem4);
vm.DropDownSelectList.Add(listItem5);
vm.DropDownSelectList.Add(listItem6);
vm.DropDownSelectList.Add(listItem7);
}
[HttpPost]
public ActionResult Admin(ChallengesAdminViewModel vm)
{
if (ModelState.IsValid)//Due to the null dropdownlist gives model state invalid
{
}
}
ViewModel-
public class ChallengesAdminViewModel
{
[Required]
public string ApiKey { get; set; }
[Required]
public string UserId { get; set; }
public List<SelectListItem> DropDownSelectList { get; set; }
}
I dont know why it still requires the list although not required. I want to have only two attributes as required. So I wanted to know how do i declare or change that list to be not required and have my Model State Valid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我这样做的方法是在我的视图模型中有一个属性,下拉列表将绑定到该属性(可以为空),然后有一个单独的属性,其中包含下拉列表的所有选项。
ViewModel 属性
视图
注意:
"N/A"
字符串是默认的空值。HTH,
查尔斯
·诗篇。这当然是使用 ASP.NET MVC 2
The way I do it is have a property in my view model that the dropdown will be bound to (which is nullable) and then have a separate property that contains all the options for the drop down.
ViewModel properties
View
Note: The
"N/A"
string is the default empty value.HTHs,
Charles
Ps. This is of course using ASP.NET MVC 2
简单的答案是将其从模型中删除并通过 ViewData 传递。然后,您的模型将包含从列表中选择的值的成员。
The simple answer would be to remove it from your model and pass it via ViewData instead. Then, your model would contain a member for the value that was selected from the list.
我认为它失败了,因为它无法将单个值 (
""
) 强制到SelectListItem
中以放入列表中。您只能选择一个DropDownSelectList
值。您可以尝试将
ChallengesAdminViewModel.DropDownSelectList
设置为字符串类型。然后它将是所选选项的值。SelectListItems
用于将选项推送到视图,而不是解析发布的结果。I think it's failing because it can't coerce a single value (
""
) into aSelectListItem
to put into a list. You can only ever have oneDropDownSelectList
value selected.You might try making your
ChallengesAdminViewModel.DropDownSelectList
of type string instead. Then it will be the value of the selected option.SelectListItems
are made for pushing options to the view, not parsing the posted result.