【发布时间】:2014-09-23 09:29:31
【问题描述】:
我正在尝试在我的 asp.net mvc 4 应用程序中创建一个动态控件。我想要的是当我提交时,我想验证必填字段。因此,可以说创建了字段类型 Checkbox 并且它是强制性的。我想确保,这是在提交之前检查的。我需要 jquery 来验证还是可以通过任何其他方式来完成?
查看模型
public class SignupViewModel : IValidatableObject
{
public List<MembershipControls> Controls { get; set; }
public List<Groups> Groups { get; set; }
}
型号
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; }
}
查看
@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>
}
【问题讨论】:
-
您尝试做的事情毫无意义...您想将 asp.mvc 视图定义移动到您奇怪的“动态控件”。不要这样做!
-
这是我构建动态控制的要求。基本上我需要为用户提供选项来定义他们想要在表单上构建什么样的控件。
标签: jquery asp.net-mvc-4 validation razor