I am trying to create a dynamic control in my asp.net mvc 4 application. What I want is when I submit, I want to validate mandatory fields. So lets say there is field type Checkbox is created and its mandatory. I want to ensure, this is checked before submitting. Do I need jquery to validate or it can be done by any other means?
View Model
public class SignupViewModel : IValidatableObject
{
public List<MembershipControls> Controls { get; set; }
public List<Groups> Groups { get; set; }
}
Model
public class Groups
{
public virtual int Id { get; set; }
public virtual string GroupTitle { get; set; }
}
public class MembershipControls
{
public virtual int Id { get; set; }
public virtual string UserId { get; set; }
public virtual string ControlType { get; set; }
public virtual string Caption { get; set; }
public virtual string Name { get; set; }
public virtual string Mandatory { get; set; }
public virtual string Content { get; set; }
public virtual string GroupTitle { get; set; }
public virtual string RadioButtonOptions { get; set; }
public virtual string SelectOptionValues { get; set; }
public virtual string SelectOptionText { get; set; }
}
View
@foreach (var groups in Model.Groups)
{
<label style="font-weight:bold">@groups.GroupTitle</label>
<div style=" border: 1px solid #CCCCCC;padding:5px">
@foreach (var row in Model.Controls.Where(r => r.GroupTitle == groups.GroupTitle))
{
<div style="padding:7px">
@if (row.ControlType == "Single Line Text")
{
<label>@row.Caption</label>
<input type="text" name="@row.Name" />
}
else if (row.ControlType == "Multi Line Text")
{
<label>@row.Caption</label>
<textarea name="@row.Name"></textarea>
}
else if (row.ControlType == "Yes/No Choice(Radio Buttons)")
{
<div>
<label>@row.Caption</label>
 
<input type="radio" name="@row.Name" value="Yes" />   Yes
 
<input type="radio" name="@row.Name" value="No" />   No
</div>
}
else if (row.ControlType == "Checkbox")
{
<div>
<input type="checkbox" name="@row.Name"/> @row.Caption
</div>
}
else if (row.ControlType == "Date")
{
<div>
<label>@row.Caption</label>
<input type="date" name="@row.Name"/>
</div>
}
</div>
}
</div>
}