与ListBoxFor&的ASP.NET MVC模型绑定DropDownList对于助手 [英] ASP.NET MVC Model Binding With ListBoxFor & DropDownListFor Helpers

查看:64
本文介绍了与ListBoxFor&的ASP.NET MVC模型绑定DropDownList对于助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号:

[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

在控制器中,将ViewData ["osTypes"]设置为具有从WebAPI提取的一些默认值的列表:

In the controller ViewData["osTypes"] is set to a List with a few default values pulled from a 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;

将ViewData ["appList"]发送到一个空列表,如下所示:

The ViewData["appList"] is sent to an empty list as follows:

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

Jquery to add item to listbox:

$("#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.

推荐答案

首先,为了使模型绑定程序起作用,所有属性都需要具有getter和setter.

First of all, in order for model binder to work all your properties need to have getters and setters.

所以改变:

public string[] applications;

收件人:

public string[] applications { get; set; }

为了使ListBox正确显示数据,请使用

And in order for ListBox to show the data correctly use

@Html.ListBoxFor(m => m.applications, 
    new MultiSelectList((List<SelectListItem>)ViewData["appList"], "Value", "Text"), 
    new {style="display:block;"})

这篇关于与ListBoxFor&amp;的ASP.NET MVC模型绑定DropDownList对于助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆