c# - 如何从客户端使用 Html 验证而不在 View Model 中进行验证?
问题描述
我只是剃须刀的新手。在使用 Html 标签助手时,我意识到验证是从 ViewModel 完成的。我的代码看起来像:-
@using (Html.BeginForm("UpdateServiceClientInformation", "contracts", new { id = Model.Id }, FormMethod.Post, new { role = "form" }))
{
<div id="clientInformationInputsWrapper">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(m => m.Client.FirstName)
@Html.TextBoxFor(m => m.Client.FirstName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Client.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(m => m.Client.LastName)
@Html.TextBoxFor(m => m.Client.LastName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Client.LastName, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(m => m.Client.Phone)
@Html.TextBoxFor(m => m.Client.Phone, new { @class = "form-control", @placeholder = "5555555555" })
@Html.ValidationMessageFor(m => m.Client.Phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(m => m.Client.Email)
@Html.TextBoxFor(m => m.Client.Email, new { @class = "form-control", @placeholder = "example@mail.com" })
@Html.ValidationMessageFor(m => m.Client.Email, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(m => m.Client.StreetOne)
@Html.TextBoxFor(m => m.Client.StreetOne, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Client.StreetOne, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(m => m.Client.AptSuite)
@Html.TextBoxFor(m => m.Client.AptSuite, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Client.AptSuite, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(m => m.Client.City)
@Html.TextBoxFor(m => m.Client.City, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Client.City, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(m => m.Client.State)
@Html.DropDownListFor(m => m.Client.State, statesList, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Client.State, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
@Html.LabelFor(m => m.Client.Zip)
@Html.TextBoxFor(m => m.Client.Zip, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Client.Zip, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<input type="submit" class="btn btn-sm btn-primary pull-right" value="Save Changes" />
}
我的视图模型看起来像:-
public class ServiceClientViewModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string StreetOne { get; set; }
public string StreetTwo { get; set; }
public string AptSuite { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Notes { get; set; }
}
我需要的视图模型验证是:-
public class ServiceClientFormViewModel
{
[Required, Display(Name = "First Name")]
public string FirstName { get; set; }
[Required, Display(Name = "Last Name")]
public string LastName { get; set; }
[Required, Display(Name = "Phone"), RegularExpression(RegexConstants.PhoneNumber, ErrorMessage = RegexConstants.PhoneNumbermessage)]
public string Phone { get; set; }
[Display(Name = "Email"), DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required, Display(Name = "Street 1")]
public string StreetOne { get; set; }
//[Display(Name = "Street 2")]
//public string StreetTwo { get; set; }
[Display(Name = "Apt/Suite#")] //Required,
public string AptSuite { get; set; }
[Required, Display(Name = "City")]
public string City { get; set; }
[Required, Display(Name = "State")]
public string State { get; set; }
[Required, Display(Name = "Zip"), RegularExpression(RegexConstants.ZipCode, ErrorMessage = RegexConstants.ZipCodeMessage)]
public string Zip { get; set; }
[Display(Name = "Ticket #")]
public string Notes { get; set; }
[Display(Name = "Company Contact Information")]
public string DeviceRegistrationInformation { get; set; }
[Display(Name = "System Information (Panel Type, Installer Code, etc)")]
public string SpecialInstruction { get; set; }
}
但是我不能在cshtml页面中使用第二个视图模型(ServiceClientFormViewModel),但我需要像那样验证。我们应该怎么做?
解决方案
页面顶部的模型Razor
应该ServiceClientFormViewModel
在您应该使用的页面底部
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
推荐阅读
- javascript - 如何在 OpenLayer 6.5 中从 JavasSript 对象创建 VectorSource?
- python - 如何使用射线并行映射生成器?
- r - 获取R中data.frame中多列的数学运算结果
- python-3.x - 一个一个地读取文件夹中的文件夹
- javascript - 使用变量编写十六进制转义字符序列
- ios - UIKit - 向按钮添加图层效果删除图像
- networking - 连接到同一网络中的 SSH
- java - 如何使用 Android Studio 通过 Android 应用程序发送电子邮件
- python - 为什么这段代码不使用海龟库在 python 中打印一个圆圈?
- html - 如何从嵌套元素中获取文本