Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

c# - ASP.NET MVC Model Binding With ListBoxFor & DropDownListFor Helpers

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:

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"] = 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:

enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

So change:

public string[] applications;

To:

public string[] applications { get; set; }

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;"})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...