好吧,我对此毫无用处(请参阅前面的问题)
为什么,当我使用以下内容时,我会得到一个可爱的复选框列表,它工作得很好:(使用扩展方法)
模型
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; }
}
看法
<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扩展(我知道那里的命名很糟糕)
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());
}
控制器
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});
}
然而,当我执行 HttpPost 操作结果时,MyTypeListModel '失去'它的属性,即我可以看到,说已经进行了两个选择,但它们的名称、值、id 不存在。
[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");
}
}
我是否必须重新转换为类型,如果需要,在哪里?
像往常一样,许多人为我的漫无边际的话题道歉。希望事情会很快开始点击:'(