使用复选框以复杂的视图模型MVC3 [英] MVC3 using CheckBox with a complex viewmodel
问题描述
右键家伙。我需要你的大脑,因为我无法找到一个方法来正确地做到这一点。
Right guys. I need your brains as I can't find a way to do this properly.
我有一个视图模型:
public class EditUserViewModel
{
public User User;
public IQueryable<ServiceLicense> ServiceLicenses;
}
用户是不重要的,因为我知道如何对付它。
User is unimportant as I know how to deal with it.
ServiceLicenses具有以下实现:
ServiceLicenses has the following implementation:
public class ServiceLicense
{
public Guid ServiceId { get; set; }
public string ServiceName { get; set; }
public bool GotLic { get; set; }
}
获取用户的检查清单是很酷。它的工作原理就像一个魅力。
Getting a checked list of users is cool. It works like a charm.
<fieldset>
<legend>Licenses</legend>
@foreach (var service in Model.ServiceLicenses)
{
<p>
@Html.CheckBoxFor(x => service.GotLic)
@service.ServiceName
</p>
}
</fieldset>
我有越来越更新ServiceLicenses新签服务对象回到我的控制器中HttpPost问题。为简单起见可以说,它看起来是这样的:
The problem I'm having is getting the updated ServiceLicenses object with new checked services back to the HttpPost in my controller. For simplicity lets say it looks like this:
[HttpPost]
public ActionResult EditUser(Guid id, FormCollection collection)
{
var userModel = new EditUserViewModel(id);
if (TryUpdateModel(userModel))
{
//This is fine and I know what to do with this
var editUser = userModel.User;
//This does not update
var serviceLicenses = userModel.ServiceLicenses;
return RedirectToAction("Details", new { id = editUser.ClientId });
}
else
{
return View(userModel);
}
}
我知道我使用复选框以错误的方式。我需要什么改变来获得serviceLicenses与表单检查框更新?
I know I am using CheckBox the wrong way. What do I need to change to get serviceLicenses to update with the boxes checked in the form?
推荐答案
据我所知,ServiceLicenses属性是一个集合,你想MVC粘结剂其绑定到你动作参数属性。对,你应该在你的看法与输入连接索引例如
i understand that ServiceLicenses property is a collection and you want MVC binder to bind it to you action parameters property. for that you should have indices attached with inputs in your view e.g
<input type="checkbox" name = "ServiceLicenses[0].GotLic" value="true"/>
<input type="checkbox" name = "ServiceLicenses[1].GotLic" value="true"/>
<input type="checkbox" name = "ServiceLicenses[2].GotLic" value="true"/>
preFIX可能不是强制性的,但结合的操作方法参数的集合属性时,它是非常方便的。为此,我建议使用循环,而不是的foreach和使用Html.CheckBox帮手,而不是Html.CheckBoxFor
Prefix may not be mandatory but it is very handy when binding collection property of action method parameter. for that purpose i would suggest using for loop instead of foreach and using Html.CheckBox helper instead of Html.CheckBoxFor
<fieldset>
<legend>Licenses</legend>
@for (int i=0;i<Model.ServiceLicenses.Count;i++)
{
<p>
@Html.CheckBox("ServiceLicenses["+i+"].GotLic",ServiceLicenses[i].GotLic)
@Html.CheckBox("ServiceLicenses["+i+"].ServiceName",ServiceLicenses[i].ServiceName)//you would want to bind name of service in case model is invalid you can pass on same model to view
@service.ServiceName
</p>
}
</fieldset>
不使用强类型的帮手只是个人的$ P $这里pference。如果你不希望索引你的投入是这样,你也可以看看这个伟大的后通过史蒂夫senderson
Not using strongly typed helper is just a personal preference here. if you do not want to index your inputs like this you can also have a look at this great post by steve senderson
编辑:我的博客有关创建于asp.net MVC3主从形式,它是有关的列表绑定以及情况。
i have blogged about creating master detail form on asp.net mvc3 which is relevant in case of list binding as well.
这篇关于使用复选框以复杂的视图模型MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!