I need to use remote validation on my boolean field. I have view model which look like this:
public class ViewModel
{
[Remote("ValidMePlease", "Home", AdditionalFields = "SomeKindOfDate", ErrorMessage = "use date with true")]
public bool IsSomething {get;set;}
public DateTime? SomeKindOfDate {get;set;}
}
My Controller:
[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);
}
}
My view:
<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>
My rendered view:
<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>
Idea is that when boolean field has true I need to require a date. And what's the problem. When I submit form it always pass false for IsSomething in my ValidMePlease remote method. I think it happens because only first input has data-val- attributes. Any suggestion will be appreciated.