用户需要选择卖家类型才能注册帐户。
如果用户选择“私人”,则ROC输入无需输入数据。但如果用户选择“公司”,则用户需要在ROC中输入数据
所以我做的是,如果用户选择卖家类型为'公司',我会在Jquery的视图中隐藏名为 rocDiv 的div。
在服务器端,我还会检查双重确认。
问题是,如果用户选择卖方类型为“私有”,则ModelState.IsValid属性保持返回false。
此处为代码段
模型 CreateNiagaModel
public class CreateNiagaModel
{
[DisplayName("Merchant Name : ")]
[Required]
[MaxLength(20)]
public string merchant { get; set; }
[DisplayName("Merchant Description : ")]
[Required]
[DataType(DataType.MultilineText)]
[MaxLength(250)]
public string merchant_desc { get; set; }
[DisplayName("Seller Type : ")]
[Required]
public string sellerType { get; set; }
[DisplayName("ROC : ")]
public string roc{ get; set; }
}
浏览
@{
List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem
{
Text = "Private",
Value = "private"
});
listItems.Add(new SelectListItem
{
Text = "Company",
Value = "company"
});
}
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
@Html.LabelFor(model => model.merchant, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.merchant, new { htmlAttributes = new { @class = "form-control", @placeholder = "Your business name", @title = "Testing" } })
@Html.ValidationMessageFor(model => model.merchant, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.merchant_desc, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.merchant_desc, new { htmlAttributes = new { @class = "form-control", @placeholder = "Explain about your business", @rows = 5 } })
@Html.ValidationMessageFor(model => model.merchant_desc, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.sellerType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.sellerType, listItems, "--Select Type--", new { @class = "form-control", @id = "sellType" })
@Html.ValidationMessageFor(model => model.sellerType, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group" id="rocDiv">
@Html.LabelFor(model => model.roc, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.roc, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.roc, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Register" class="btn btn-success" />
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$('#rocDiv').hide();
$('#sellType').change(function () {
var value = $(this).val();
if (value == 'company') {
$('#rocDiv').show();
} else {
$('#rocDiv').hide();
}
});
</script>
}
控制器
[HttpPost]
public ActionResult Register(CreateNiagaModel CN)
{
if (ModelState.IsValid)
{
//Do some logic
}
else
{
//Code always go here if user select 'private' in seller type
}
}
我该如何解决这个问题?或者我可以做任何其他方式吗?抱歉我的英文不好
答案 0 :(得分:1)
您可以实施IValidatableObject
界面。
public class CreateNiagaModel : IValidatableObject
{
[DisplayName("Merchant Name : ")]
[Required]
[MaxLength(20)]
public string merchant { get; set; }
[DisplayName("Merchant Description : ")]
[Required]
[DataType(DataType.MultilineText)]
[MaxLength(250)]
public string merchant_desc { get; set; }
[DisplayName("Seller Type : ")]
[Required]
public string sellerType { get; set;
[DisplayName("ROC : ")]
public string roc{ get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext ctx)
{
if (sellerType == "company" && string.IsNullOrEmpty(roc))
{
yield return new ValidationResult("ROC is required when seller type is Company", new[] { "roc" });
}
}
}
此验证逻辑由MVC在验证视图模型时执行,如果在此处使用相同模型,则在保存更改时由实体框架执行。
编辑:我重读了你的问题,我可能第一次误解了它。当卖家类型为“私人”时,ModelState.IsValid
看起来应该是真的。我会在if (ModelState.IsValid)
行设置断点并检查错误,以确切了解导致验证失败的原因。