Sunday 15 February 2015

asp.net mvc - MVC MultiSelectList Binding -



asp.net mvc - MVC MultiSelectList Binding -

i utilize asp.net mvc .. when post form it's raise cast error when model validate. how can fixed view model or validation way?

"the parameter conversion type 'system.string' type 'system.web.mvc.selectlistitem' failed because no type converter can convert between these types." give thanks you..

//my view model public class prodgroupviewmodel { //i've fixed here or way? public ienumerable<selectlistitem> rooms { get; set; } } //controller public actionresult create(int id) { homecoming view(new prodgroupviewmodel { rooms = new multiselectlist(_roomservice.getall(), "roomid", "roomname"), }); } //in view <div class="form-group"> <label class="col-md-3 control-label">oda</label> <div class="col-md-9"> @html.listboxfor(model => model.rooms, (multiselectlist)model.rooms, new { @class = "form-control" }) </div> </div>

you're trying post same property holds select list. posted result of selections in listbox comma-delimited string of selected alternative values, modelbinder incapable of binding property of type multiselectlist.

you need additional model property hold posted value like:

public list<int> selectedroomids { get; set; }

and in view:

@html.listboxfor(m => m.selectedroomids, model.rooms, new { @class = "form-control" })

also, don't need cast model.rooms, since it's strongly-typed.

asp.net-mvc validation razor

No comments:

Post a Comment