C# 远程验证在ASP.NET MVC 5中不起作用
在这里,我创建了一个表单,该表单包含客户电子邮件字段,我试图检查输入的电子邮件是否已存在,如果存在,则显示电子邮件已存在消息。C# 远程验证在ASP.NET MVC 5中不起作用,c#,entity-framework,asp.net-mvc-5,remote-validation,C#,Entity Framework,Asp.net Mvc 5,Remote Validation,在这里,我创建了一个表单,该表单包含客户电子邮件字段,我试图检查输入的电子邮件是否已存在,如果存在,则显示电子邮件已存在消息。 为此,我尝试使用远程验证,但问题是,即使存在电子邮件,它也不会显示任何错误,甚至不会在用于远程验证的IsEmailExists方法中击中控制器 对我的代码的任何帮助都将是巨大的帮助。谢谢你 以下是我在控制器中的操作 public JsonResult IsEmailExists(string CustomerEmail) { emedic
为此,我尝试使用远程验证,但问题是,即使存在电子邮件,它也不会显示任何错误,甚至不会在用于远程验证的
IsEmailExists
方法中击中控制器对我的代码的任何帮助都将是巨大的帮助。谢谢你
以下是我在控制器中的操作
public JsonResult IsEmailExists(string CustomerEmail)
{
emedicineEntities _db = new emedicineEntities();
return Json(!_db.Customers.Any(x => x.CustomerEmail == CustomerEmail), JsonRequestBehavior.AllowGet);
}
下面是我的元数据
namespace eMedicine.Model
{
public class CustomerMetaDta
{
[Remote("IsEmailExists", "Customers", ErrorMessage = "EmailId already exists.")]
[Required(ErrorMessage = "Please Enter Emailw")]
public string CustomerEmail { get; set; }
}
}
下面是我的部分课程
namespace eMedicine.Model
{
[MetadataType(typeof(CustomerMetaDta))]
public partial class Customer
{
}
}
以下是我对客户电子邮件的看法
<link href="~/Content/Site.css" rel="stylesheet" />
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm("Create", "Customers", FormMethod.Post, new { @id = "register" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustomerName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CustomerEmail, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CustomerEmail, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustomerEmail, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PasswordHash, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PasswordHash, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PasswordHash, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
@使用(Html.BeginForm(“Create”、“Customers”、FormMethod.Post、new{@id=“register”}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.CustomerName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.CustomerName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.CustomerName,“,new{@class=“text danger”})
@LabelFor(model=>model.CustomerEmail,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.CustomerEmail,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.CustomerEmail,“,new{@class=“text danger”})
@LabelFor(model=>model.PasswordHash,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.PasswordHash,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.PasswordHash,“,new{@class=“text danger”})
}
如下更改方法签名,以包括绑定前缀
属性/属性
public JsonResult IsEmailExists([Bind(Prefix="Customer.CustomerEmail")] string CustomerEmail)
{
emedicineEntities _db = new emedicineEntities();
return Json(!_db.Customers.Any(x => x.CustomerEmail == CustomerEmail), JsonRequestBehavior.AllowGet);
}
现在应该可以了 我不确定你的源代码到底有什么问题,但我试着在我这边重现,效果很好 这是我的源代码
namespace WebApplication1.Controllers
{
public class CustomerMetaDta
{
[Remote("IsEmailExists", "Customer", ErrorMessage = "EmailId already exists.")]
[Required(ErrorMessage = "Please Enter Emailw")]
public string CustomerEmail { get; set; }
}
[MetadataType(typeof(CustomerMetaDta))]
public partial class Customer
{
}
public partial class Customer
{
public string CustomerEmail { get; set; }
public string CustomerName { get; set; }
public string PasswordHash { get; set; }
}
public class CustomerController : Controller
{
public JsonResult IsEmailExists(string CustomerEmail)
{
//emedicineEntities _db = new emedicineEntities();
List<Customer> _db = new List<Customer>
{
new Customer { CustomerEmail = "hien@gmail.com"},
new Customer { CustomerEmail = "hien1@gmail.com"}
};
return Json(!_db.Any(x => x.CustomerEmail == CustomerEmail), JsonRequestBehavior.AllowGet);
}
// GET: Customer
public ActionResult Index()
{
return View();
}
}
}
命名空间WebApplication1.控制器
{
公共类客户机
{
[远程(“IsEmailExists”,“Customer”,ErrorMessage=“EmailId已存在。”)]
[必需(ErrorMessage=“请输入Emailw”)]
公共字符串CustomerEmail{get;set;}
}
[元数据类型(typeof(customerMetadata))]
公共部分类客户
{
}
公共部分类客户
{
公共字符串CustomerEmail{get;set;}
公共字符串CustomerName{get;set;}
公共字符串密码哈希{get;set;}
}
公共类CustomerController:控制器
{
public JsonResult IsEmailExists(字符串CustomerEmail)
{
//emedicineEntities _db=新的emedicineEntities();
列表_db=新列表
{
新客户{CustomerMail=”hien@gmail.com"},
新客户{CustomerMail=”hien1@gmail.com"}
};
返回Json(!\u db.Any(x=>x.CustomerEmail==CustomerEmail),JsonRequestBehavior.AllowGet);
}
//获取:客户
公共行动结果索引()
{
返回视图();
}
}
}
Index.cshtml文件:
@model WebApplication1.Controllers.Customer
@{
ViewBag.Title = "Index";
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.11/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm("Create", "Customer", FormMethod.Post, new { @id = "register" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustomerName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CustomerEmail, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CustomerEmail, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustomerEmail, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PasswordHash, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PasswordHash, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PasswordHash, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
@model WebApplication1.Controllers.Customer
@{
ViewBag.Title=“Index”;
}
@使用(Html.BeginForm(“Create”、“Customer”、FormMethod.Post、new{@id=“register”}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.CustomerName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.CustomerName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.CustomerName,“,new{@class=“text danger”})
@LabelFor(model=>model.CustomerEmail,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.CustomerEmail,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.CustomerEmail,“,new{@class=“text danger”})
@LabelFor(model=>model.PasswordHash,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.PasswordHash,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.PasswordHash,“,new{@class=“text danger”})
}
它跳转到方法IsEmailExists(),这是结果输出
也许你错过了设置
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
在web.config中?如果没有击中控制器,则表明这不是实际验证逻辑的问题,而是如何寻址服务器的问题 有几件事需要检查: 客户端是否可以使用您的远程验证代码? 首先要考虑的是这可能是一个安全/认证问题。您可以做一些简单的事情来检查:
- 如果在控制器或方法上设置了身份验证属性,请尝试注释它们
- 尝试注释掉任何其他身份验证代码
- 邮递员从你的支票上拿回200英镑
[System.Web.Mvc.Remote( action: "CheckExistingDocumentCode", controller: "Documents", AdditionalFields = "DocumentId", HttpMethod = "POST", ErrorMessage = "Code already exists")] public string DocumentCode { get; set; }
[HttpPost] public async Task<ActionResult> CheckExistingDocumentCode(string DocumentCode, int DocumentId) { try { if (!await _documentValidationRules.IsExistingDocumentCodeAsync(DocumentCode, DocumentId)) { return Json(true, JsonRequestBehavior.AllowGet); } return Json("This Document Code is already in use", JsonRequestBehavior.AllowGet); } catch (Exception ex) { return Json(ex.ToString(), JsonRequestBehavior.AllowGet); } }
@section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }