May 29, 2014, 10:20 PM
[CLOSED] MVC conditional validation not showing error
Hi, I have a grid that uses popup editor to insert new row. I created a RequiredIfValidator and attach it to model field :
Did I missed something? what is best way to do conditional validation? I am using MVC 4
Thanks
-susan
public Class JobModel
{
[DisplayName("Email")]
public bool SendEmailFlag { get; set; }
[DisplayName("Subject")]
[StringLength(80)]
[RequiredIf("SendEmailFlag", true, ErrorMessage = "You must specify the Email Subject if Send Email is true")]
public string EmailSubject { get; set; }
}
cshtml:@(Html.Kendo().Grid<JobModel> ()
.Name("JobGrid")
.HtmlAttributes(new { style = "height:700px;" })
.Editable(editing => editing.Mode(GridEditMode.PopUp)
.CreateAt(0)
.DisplayDeleteConfirmation (false))
.ToolBar(toolbar => toolbar.Create().Text("Add"))
.Columns(columns =>
{
columns.Bound(e => e.JobID).Hidden(true);
columns.Bound(e => e.JobDesc);
columns.Bound(e => e.SendEmailFlag)
.HeaderTemplate(@<text>
<a href="#" data-toggle="tooltip" title="Email Notification Enabeld" tabindex ="-1" >
<img src = "Images/Email.icon.png" /> </a>
</text>
).Width("4%")
.Filterable(false)
.ClientTemplate("<input type='checkbox' disabled='disabled' name='SendEmailFlag' #: SendEmailFlag? checked='checked' : '' # />")
.HtmlAttributes(new { style = "text-align:center" });
columns.Bound(e => e.Runnable)
.HeaderTemplate(@<text>
<a href="#" data-toggle="tooltip" title="Ready to distribute -- has at least one group assigned." tabindex ="-1" >
<img src = "Images/ready.jpg" /> </a>
</text>
).Width("4%")
.Filterable(false)
.ClientTemplate("<input type='checkbox' disabled='disabled' name='Runnable' #: Runnable? checked='checked' : '' # />")
.HtmlAttributes(new { style = "text-align:center" });
columns.Bound(e => e.CreateBy);
columns.Bound(e => e.LastModify).Format("{0:g}");
columns.Template(x => { })
.ClientTemplate("<a class='k-button' href='" + Url.Action("AddReport", "Job") + "/#=JobID#'>Assign Report</a> ");
columns.Command(commands =>
{
commands.Edit();
commands.Destroy();
});
})
.Events (ev =>
{ ev.DataBound ("OnGridDataBound");
ev.Edit("OnEdit");
})
.ClientDetailTemplateId("JobDetailTemplate")
.DataSource(dataBinding => dataBinding.Ajax()
.PageSize(20)
.Model(model =>
{ model.Id(p => p.JobID);
model.Field(p => p.JobID).DefaultValue(Guid.Empty);
model.Field(p => p.CreateTime).Editable(false);
model.Field(p => p.LastModify).Editable(false);
})
.Read(read => read.Action("Job_Read", "Job"))
.Sort(sort => sort.Add(job => job.JobDesc))
.Update(update => update.Action("Job_Update", "Job"))
.Create(update => update.Action("Job_Insert", "Job"))
.Destroy(update => update.Action("Job_Delete", "Job"))
)
.Pageable(p => p.Enabled(true).ButtonCount(5).PageSizes(new int[] {20, 40, 60, 100}).Refresh(true))
.Filterable(f => f.Extra(false))
.Resizable(resize => resize.Columns(true))
.Sortable()
.Scrollable()
)
The Validate() returns error but it never show the error under the filed like other Required validator. Did I missed something? what is best way to do conditional validation? I am using MVC 4
Thanks
-susan
Last edited by Daniil; Jun 06, 2014 at 6:30 PM.
Reason: Please use [CODE] tags, [CLOSED]