Right ok, I’m offically useless at this (see previous questions)
Why, when I use the following do I get a lovely checkboxlistfor that works all nicely:
(using a extension method)
Model
public class My : BusinessCategory
{
public class MyTypes
{
public int MyTypeId { get; set; }
public string MyTypeName { get; set; }
public bool? MyTypeValue { get; set; }
}
public List<MyTypes> MyTypeListModel { get; set; }
}
View
<fieldset>
<legend>My Management</legend>
<div style="text-align: left; padding-left: 47%;">
@Html.CheckBoxListForListType(model => model.MyTypeListModel, (MultiSelectList)ViewBag.MyType, Model.ReviewId)
</div>
<p>
<input type="submit" value="Continue" />
</p>
</fieldset>
CheckBoxListForListType Extension (awful naming there I know)
public static MvcHtmlString CheckBoxListForListType<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, IEnumerable<TProperty>>> expression, MultiSelectList multiSelectList, Guid reviewId, object htmlAttributes = null)
{
//Derive property name for checkbox name
MemberExpression body = expression.Body as MemberExpression;
string propertyName = body.Member.Name;
//Get currently select values from the ViewData model
IEnumerable<TProperty> list = expression.Compile().Invoke(htmlHelper.ViewData.Model);
//Convert selected value list to a List<string> for easy manipulation
List<string> selectedValues = new List<string>();
if (list != null)
{
selectedValues = new List<TProperty>(list).ConvertAll<string>(delegate(TProperty i) { return i.ToString(); });
}
//Create div
TagBuilder divTag = new TagBuilder("div");
divTag.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);
//Add checkboxes
foreach (SelectListItem item in multiSelectList)
{
divTag.InnerHtml += String.Format("<div><input type=\"checkbox\" name=\"{0}\" id=\"{0}_{1}\" " +
"value=\"{1}\" {2} /><label for=\"{0}_{1}\">{3}</label></div>",
propertyName,
item.Value,
GetValueFromDatabaseList(reviewId, propertyName, item.Text.ToLower()),
item.Text);
}
return MvcHtmlString.Create(divTag.ToString());
}
Controller
public ActionResult My(Guid id)
{
try
{
var model = Model(id);
SetMyTypeList(model.My);
ViewBag.MyType = new MultiSelectList(model.My.MyTypeListModel, "MyTypeValue", "MyTypeName");
return View(model.My);
}
catch (Exception ex)
{
ExceptionHelper.WriteLog(ex);
return RedirectToAction("Error");
}
}
private void SetMyTypeList(My model)
{
model.MyTypeListModel = new List<My.MyTypes>();
model.MyTypeListModel.Add(new My.MyTypes { MyTypeId = 1, MyTypeName = GetName.GetDisplayName(model, m => m.Option1), MyTypeValue = model.Option1});
model.MyTypeListModel.Add(new My.MyTypes { MyTypeId = 2, MyTypeName = GetName.GetDisplayName(model, m => m. Option2), MyTypeValue = model.Option2 });
model.MyTypeListModel.Add(new My.MyTypes { MyTypeId = 3, MyTypeName = GetName.GetDisplayName(model, m => m. Option3), MyTypeValue = model.Option3});
model.MyTypeListModel.Add(new My.MyTypes { MyTypeId = 4, MyTypeName = GetName.GetDisplayName(model, m => m.Option4), MyTypeValue = model.Option4});
}
Yet when I perform a HttpPost action result, MyTypeListModel ‘loses’ it’s properties i.e. I can see that, say two selections have been made but their names, values, id’s are not present.
[HttpPost]
public ActionResult My(Guid id, My model)
{
try
{
model.ReviewId = id;
var sessionModel = Model(id);
sessionModel.My = model;
MapCheckBoxListResultswithDatabaseBoolsMy(model);
UpdateDb(Categories.catMy, sessionModel);
return RedirectToAction("BusinessSummary", new { id = sessionModel.ReviewId });
}
catch (Exception ex)
{
ExceptionHelper.WriteLog(ex);
return RedirectToAction("Error");
}
}
Do I have to recast to the type and if so where?
As per usual, many apologies for what appears to be a continuous thread of my ramblings. Hopefully things will start to click soon :'(
The process of wiring up you action parameters is called model binding, and by default MVC interrogates the form, query string and route for the data to try and fill your type. In your case the problem will be that your form fields are not named in the default way that it expects for a list. You can find some information on binding lists in this article by Phil Haack. Keep in mind that you are looking for a subproperty so you need to prefix the type appropriately.
One thing that I have found useful is to get the source code to ASP.NET MVC and then debug that locally to see what it is looking for when model binding.
Don’t forget that if you don’t want to follow the normal conventions (although that is sensible) you can specify your own custom model binder for your type.