C# 使用 ListBoxFor 和 DropDownListFor Helpers 绑定 ASP.NET MVC 模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19143126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC Model Binding With ListBoxFor & DropDownListFor Helpers
提问by vesuvious
I have the following Model:
我有以下型号:
[Required (ErrorMessage="Server Name Required")]
[StringLength(15, ErrorMessage = "Server Name Cannot Exceed 15 Characters")]
public string servername { get; set; }
[Required(ErrorMessage = "Server OS Type Required")]
public string os { get; set; }
public string[] applications;
And I have used the following code to bind a textbox to the servername which works fine:
我使用以下代码将文本框绑定到服务器名称,效果很好:
@Html.TextBoxFor(m => Model.servername, new {@class="fixed", @id="serverName"})
I am using a dropdown list for the OS, and a listbox for applications, both of which do not populate the model correctly on submit.
我正在使用操作系统的下拉列表和应用程序的列表框,两者都没有在提交时正确填充模型。
@Html.DropDownListFor(m => m.os , new SelectList( ((List<SelectListItem>)ViewData["osTypes"]),"Value","Text"), new { @class = "fixed" })
@Html.ListBoxFor(m => m.applications, new MultiSelectList((List<SelectListItem>)ViewData["appList"]), new {style="display:none;"})
Any thoughts on what I am doing wrong here?
关于我在这里做错了什么的任何想法?
Update: I don't think I gave enough information here
更新:我不认为我在这里提供了足够的信息
In the controller ViewData["osTypes"] is set to a List with a few default values pulled from a WebAPI:
在控制器 ViewData["osTypes"] 中设置为一个列表,其中包含一些从 WebAPI 中提取的默认值:
List<string> osTypes = FastFWD_SITE.Helpers.GetJsonObj._download_serialized_json_data<List<string>>(getOsTypeUrl);
List<SelectListItem> osTypeList = new List<SelectListItem>();
foreach (string osType in osTypes)
{
osTypeList.Add(new SelectListItem { Text = osType });
}
ViewData["osTypes"] = osTypeList;
The ViewData["appList"] is sent to an empty list as follows:
ViewData["appList"] 被发送到一个空列表,如下所示:
ViewData["appList"] = new List<SelectListItem>();
For the list of applications, the user fills in a textbox and hits a button to add data to the application listbox:
对于应用程序列表,用户填写一个文本框并点击按钮将数据添加到应用程序列表框:
Jquery to add item to listbox:
Jquery 将项目添加到列表框:
$("#addItem").click(function () {
var txt = $("#appName");
var svc = $(txt).val(); //Its Let you know the textbox's value
var lst = $("#serverSpecification_applications");
var ul = $("#itemList");
var options = $("#serverSpecification_applications option");
var iList = $("#itemList li");
var alreadyExist = false;
$(options).each(function () {
if ($(this).val() == svc) {
alreadyExist = true;
txt.val("");
return alreadyExist;
}
});
if (!alreadyExist) {
$(lst).append('<option value="' + svc + '" selected=true>' + svc + '</option>');
$(ul).append('<li id="' + svc + '"><label>' + svc + '<input type="checkbox" id="' + svc + '" onclick="removeItem(this.id)"/></label>')
txt.val("");
return false;
}
});
I have a feeling i am doing something horribly wrong here, anything is helpful.
我有一种感觉,我在这里做错了什么,任何事情都有帮助。
采纳答案by Davor Zlotrg
First of all, in order for model binder to work all your properties need to have getters and setters.
首先,为了让模型绑定器能够工作,您的所有属性都需要有 getter 和 setter。
So change:
所以改变:
public string[] applications;
To:
到:
public string[] applications { get; set; }
And in order for ListBox to show the data correctly use
并且为了让 ListBox 正确显示数据,请使用
@Html.ListBoxFor(m => m.applications,
new MultiSelectList((List<SelectListItem>)ViewData["appList"], "Value", "Text"),
new {style="display:block;"})