正確的傢伙。我需要你的大腦,因爲我無法找到正確做到這一點的方法。MVC3使用CheckBox與複雜的viewmodel
我有一個視圖模型:
public class EditUserViewModel
{
public User User;
public IQueryable<ServiceLicense> ServiceLicenses;
}
用戶是不重要的,因爲我知道如何對付它。
ServiceLicenses具有以下實現:
public class ServiceLicense
{
public Guid ServiceId { get; set; }
public string ServiceName { get; set; }
public bool GotLic { get; set; }
}
獲取用戶的檢查清單是很酷。它像一個魅力。
<fieldset>
<legend>Licenses</legend>
@foreach (var service in Model.ServiceLicenses)
{
<p>
@Html.CheckBoxFor(x => service.GotLic)
@service.ServiceName
</p>
}
</fieldset>
我有越來越更新ServiceLicenses新籤服務對象回到我的控制器HttpPost的問題。爲了簡單起見,可以說它看起來像這樣:
[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);
}
}
我知道我使用CheckBox的方式不對。我需要更改以使serviceLicenses在表單中選中的框更新?
我用史蒂夫森德森的方法去了。我認爲爲這類事情製作一組通用的HTML幫助程序會很有用。如果時間允許,我會查看它。謝謝您的幫助。 – TheGwa 2011-04-20 07:31:22
@TheGwa肯定會很棒 – 2011-04-20 10:20:37