我需要在我的布尔字段上使用远程验证。我有看起来像这样的视图模型:
public class ViewModel
{
[Remote("ValidMePlease", "Home", AdditionalFields = "SomeKindOfDate", ErrorMessage = "use date with true")]
public bool IsSomething {get;set;}
public DateTime? SomeKindOfDate {get;set;}
}
我的控制器:
[OutputCache(Location = System.Web.UI.OutputCacheLocation.None, NoStore = true)]
public class HomeController : Controller
{
public JsonResult ValidMePlease(bool IsSomething , DateTime? SomeKindOfDate )
{
if (IsSomething && !SomeKindOfDate .HasValue)
return Json(false, JsonRequestBehavior.AllowGet);
return Json(true, JsonRequestBehavior.AllowGet);
}
}
我的观点:
<div class="control">
@Html.LabelFor(m => m.IsSomething , new { @class = "control-label" })
<div class="controls">
<label class="radio inline">
@Html.RadioButtonFor(m => m.IsSomething , false) No
</label>
<label class="radio inline">
@Html.RadioButtonFor(m => m.IsSomething, true) Yes
</label>
</div>
</div>
我的渲染视图:
<div class="control">
<label class="control-label" for="IsSomething ">Is something</label>
<div class="controls">
<label class="radio inline">
<input checked="checked" data-val="true" data-val-remote="use date with true" data-val-remote-additionalfields="*.IsSomething ,*.SomeKindOfDate" data-val-remote-url="/admin/User/ValidMePlease" data-val-required="Is something is required." id="IsSomething " name="IsSomething " type="radio" value="False" /> No
</label>
<label class="radio inline">
<input id="IsSomething " name="IsSomething " type="radio" value="True" /> Yes
</label>
</div>
</div>
想法是,当布尔字段为真时,我需要一个日期。还有什么问题。当我提交表单时,它总是在我的远程方法中传递false
。我认为这是因为只有第一个输入具有属性。任何建议将不胜感激。IsSomething
ValidMePlease
data-val-