Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
2  
Show the controller, only you know what you put in your viewBags –  meda Oct 2 '13 at 18:29

1 Answer 1

up vote 5 down vote accepted

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;"})
share|improve this answer
1  
You are amazing, I love you. –  vesuvious Oct 2 '13 at 19:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.