在Controller中编辑
创建是可以的,但是不能将db中的选定值绑定到编辑视图。它显示复选框,但它们为空。我知道这里有一点小问题是错误的,需要敏锐的眼睛来抓住它。它将对我有很大帮助。 thnx
maxWidth things = fromMaybe 0 . getMax . foldMap (Max . Just . width) $ things
修改视图
[AuthorizeRoles(RoleNames.CanEditCustomer)]
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var customer = customerManager.Get(id);
if (customer == null)
{
return HttpNotFound();
}
var vm = new CustomerViewModel();
vm.GetCustomerTypeViewModel(new List<CustomerTypeViewModel>(), customerTypeManager.GetAll());
return View(vm);
}
ViewModel
<div class="form-group">
@{
for (int i = 0; i < Model.GetCustomerType.Count(); i++)
{
<div class="col-md-10">
@Html.Label(Model.GetCustomerType[i].Description, new { @class = "control-label col-md-2" })
@Html.CheckBoxFor(model => model.GetCustomerType[i].Selected)
@Html.HiddenFor(model => model.GetCustomerType[i].Id)
@Html.HiddenFor(model => model.GetCustomerType[i].Description)
</div>
}
}
</div>
经理
public List<CustomerTypeViewModel> GetCustomerType { get; set; }
public void GetCustomerTypeViewModel(IEnumerable<CustomerTypeViewModel> selected, IEnumerable<CustomerTypeViewModel> all)
{
foreach (var item in all)
{
GetCustomerType.Add(item);
}
foreach (var item in selected)
{
GetCustomerType.FirstOrDefault(x => x.Description == item.Description).Selected = true;
}
}
我们将不胜感激任何帮助。
答案 0 :(得分:1)
您的Edit View
应该如下:
<div class="form-group">
@{
for (int i = 0; i < Model.GetCustomerType.Count(); i++)
{
<div class="col-md-10">
@Html.Label(Model.GetCustomerType[i].Description, new { @class = "control-label col-md-2" })
<input type="checkbox" name="selectedCustomerTypes" value="@Model.GetCustomerType[i].Id"
@if (Model.GetCustomerType[i].Selected)
{
<text> Checked</text>
}/>
@Html.HiddenFor(model => model.GetCustomerType[i].Id)
@Html.HiddenFor(model => model.GetCustomerType[i].Description)
</div>
}
}
</div>
这是我通常处理复选框的操作