我有一个带选项的列表框。用户可以选择全部或选择任何一个,所以我需要的是将所选内容绑定到控制器上的模型。即使我选择了一些,它也是空的。
我已经将数据填充到列表框中。 LoadAllControls方法位于索引控制器中。
将数据加载到列表框。这可以100%
public void LoadAllControls(myModel model)
{
try
{
string jsonReq = null;
string jsonResp = null;
//API call here
jsonResp = JsonWrapper.JsonGET(jsonReq);
List<Structs> listOrderTypes = DeserialiseFromJson<List<Structs>>.Deserialise(jsonResp);
List<SelectListItem> listO = listOrderTypes.Select(item => new SelectListItem
{
Value = item.orderType.ToString(),
Text = item.orderTypeName,
Selected = "-1" == (item.orderTypeCode.ToString()) ? true : false
}).ToList();
model.orderTypes = new List<SelectListItem>(listO);
}
catch(Exception exx)
{
throw exx;
}
}
查看
@using (Ajax.BeginForm("Search", "myController", new AjaxOptions { HttpMethod = "Post" }))
{
<div class="form-group">
<label class="control-label col-sm-2" for="txtfrom">Order Type:</label>
<div class="col-md-4">
@Html.ListBoxFor(m => m.orderTypesIds, Model.orderTypes, new { @class = "form-control listbox", id = "orderTypeIdList", @multiple = "multiselect" })
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<button class="btn btn-primary " id="btnSearch" name="SearchButton" type="submit">
Search
</button>
</div>
</div>
}
控制器
public ActionResult Search(myModel model)
{
//Selected options comes as null to the model
//Some logic here
}
我的模特
public class myModel
{
public List<SelectListItem> orderTypes { get; set; }
public int[] orderTypesIds { get; set; }
}