使用远程validationasp.net MVC
我想尝试使用我在此链接上发现的远程validation: http : //www.youtube.com/watch?v = L8VtDRj8L4
我已经按照说明操作了但问题是,当我尝试从引用的表中添加数据时,validation不起作用
型号类:
public partial class ms_student { public int ID { get; set; } public string student_code{ get; set; } public virtual ms_person ms_person { get; set; } } public partial class ms_person { public string name{ get; set; } public string email { get; set; } public virtual ms_student ms_student { get; set; } }
元数据:
using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; namespace Test.Models { [MetadataType(typeof(personMD))] public partial class ms_person { } public class personMD { [Required(ErrorMessage = "Email is required")] [EmailAddress(ErrorMessage = "Invalid Email Address")] [Remote("CheckEmailExist", "Administrator", ErrorMessage = "Email Already Exist")] public object email { get; set; } } }
控制器:
public JsonResult CheckEmailExist(string email) // the error i think from email paramater, cause the video said to make the paramater exactly the same name... { return Json(!db.ms_person.Any(m => m.email == email), JsonRequestBehavior.AllowGet); }
浏览次数:
@model Test.Models.ms_student @using (Html.BeginForm("CreateStudent", "Administrator", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) @Html.TextBoxFor(model => model.student_code) //this one work and already tested @Html.ValidationMessageFor(model => model.student_code) @Html.TextBoxFor(model => model.ms_person.email) //if you inspect element on browser the NAME are ms_person.email and ID are ms_person_email @Html.ValidationMessageFor(model => model.ms_person.email) }
我试图将JsonResult控制器参数更改为(字符串ms_person.email),但是有错误表示无法找到命名空间电子邮件..也尝试使用(字符串ms_person_email),也无法工作
我还使用student_code进行了测试,student_code字段正常工作,因为student_code属性属于同一模型(ms_student),而不是电子邮件(对ms_person的引用)
所有元数据validation工作,就像两个模型上所需的一样,所以我猜测错误是在JsonResult参数上
非常感谢你
更改操作方法以包括“ Bind Prefix
属性/属性
public JsonResult CheckEmailExist([Bind(Prefix="ms_person.email")]string email) { ...
只需将其更改为
@Html.TextBoxFor(model => model.ms_person.email, new{@id="email", @name="email"})
- 测试ModelState在asp.net mvc中始终有效
- 在ASP.NET MVC中调用Visual Studio 2013中的“全部中断”时,有没有办法隐藏“源不可用”选项卡?
- 如何将带有.MDF文件(Sql 2008)的现有ASP.NET MVC 3项目迁移到Windows Azure模拟器(使用SQL Azure)
- 如何使用Ajax.BeginForm更新div并执行javascript函数?
- 使用Shibboleth进行asp.net MVC身份validation
- MVC5表单,包含重复的部分视图
- 如何在vs2012中将发布配置文件添加到新的sln?
- 单个解决方案中的多个MVC项目
- 如何使用Html.GetUnobtrusiveValidationAttributes()