2
视图模型的特定属性我有一个这样的模型:MVC - 绑定基于复选框
public class EmploymentVM
{
public string EmploymentType { get; set; }
public string Employer { get; set; }
public string Position { get; set; }
public double GrossMonthlyIncome { get; set; }
public DateTime? StartDate { get; set; }
public string AutonomousActivity { get; set; }
}
在我的观点:
<div class="form-group">
@Html.LabelFor(x => x.Employer)
@Html.TextBoxFor(x => x.Employer, new { @class = "form-control required" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.Position)
@Html.TextBoxFor(x => x.Position, new { @class = "form-control required" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.GrossMonthlyIncome)
@Html.TextBoxFor(x => x.GrossMonthlyIncome, new { @class = "form-control required digits" })
</div>
等的不同的属性。
控制器是非常基本的:
[HttpPost]
public ActionResult EmploymentInfo(EmploymentVM vm)
{
//the actual code to save employment
return View();
}
的问题是:我想要的模型保存与物业AutonomousActivity
只有当用户点击复选框IsAutonomous
,或者如果复选框保存属性Employer
和Position
没有被检查。
我该如何做到这一点,模型活页夹能帮我吗? 谢谢先进。
您的视图模型有IsAutonomous? – User3250
没有。这只是CheckBox – user70014
将其添加到视图模型中,并将其用于数据的条件绑定。或者使用jquery启用禁用复选框被选中或取消选中的字段。已禁用的字段数据不会发布到操作。恕我直言,ViewModel方式更容易实现。 – User3250