帐户模型类别:
[Table("Accounts")]
public class Account
{
public int Id { get; set; }
public string CompanyName { get; set; }
public float Interval { get; set; }
}
移动模型类:
public class Mobile
{
public int Id { get; set; }
public string MobileNo { get; set; }
public virtual Account Account { get; set; }
public static Mobile Add(string mobile)
{
Mobile mobiles = new Mobile();
mobiles.MobileNo = mobile;
return mobiles;
}
}
因此,一个帐户可以有多个手机号码。现在,我创建一个可以插入多个手机号码的表单:
@model PowerSupply.Models.CompanyAccountViewModel
@{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h3>Register</h3> <br />
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group row">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "col-sm-2 col-md-1 col-form-label" })
<div class="col-sm-10 col-md-3">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
@Html.LabelFor(model => model.Interval, htmlAttributes: new { @class = "col-sm-2 col-md-1 col-form-label" })
<div class="col-sm-10 col-md-3">
@Html.EditorFor(model => model.Interval, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Interval, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
@Html.LabelFor(model => model.Mobile, htmlAttributes: new { @class = "col-sm-2 col-md-1 col-form-label" })
<div class="col-sm-10 col-md-3">
<span class="add-new-icon glyphicon glyphicon-plus-sign" id="add_mobile"> </span>
<input name="Mobile" class="form-control" id="mobile_no" />
</div>
</div>
<div class="form-group row new_mobile_wrapper">
<div class="col-md-offset-3">
<div class="new_mobile_container">
</div>
</div>
</div>
<div class="form-group row">
<div class="col-md-offset-2 col-sm-10 col-md-2">
<input type="submit" value="Register" class="btn btn-primary col-sm-offset-1" />
</div>
</div>
}
最后,我将验证设置如下:
public CompanyAccountViewModel Handle(CompanyAccountRegistrationResponseMessage message, CompanyAccountViewModel viewModel, ModelStateDictionary modelState)
{
if(!message.ValidationResult.IsValid)
{
foreach(var error in message.ValidationResult.Errors)
{
switch (error.PropertyName)
{
case "CompanyName":
modelState.AddModelError("Name",error.ErrorMessage);
break;
case "Interval":
modelState.AddModelError("Interval",error.ErrorMessage);
break;
}
}
}
return viewModel;
}
public class CompanyAccountRegistrationRequestMessageValidator : AbstractValidator<CompanyAccountRegistrationRequestMessage>
{
public CompanyAccountRegistrationRequestMessageValidator()
{
RuleFor(x=>x.CompanyName).NotEmpty().WithMessage("Please specify a Company Name");
RuleFor(x=>x.Interval).ExclusiveBetween(0,10).WithMessage("Interval value must be between 1 and 9");
}
}
这两个验证规则非常有效。我也想将验证应用于移动,即:移动电话号码应该是唯一的。我该怎么办?
修改:
public class CompanyAccountViewModel
{
public string Name { get; set; }
public float Interval { get; set; }
public List<string> Mobile { get; set; }
}