Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用远程验证asp.net MVC_C#_Asp.net Mvc_Validation - Fatal编程技术网

C# 使用远程验证asp.net MVC

C# 使用远程验证asp.net MVC,c#,asp.net-mvc,validation,C#,Asp.net Mvc,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 c

我想尝试使用我在该链接上发现的远程验证:

我已经按照说明进行了操作,但问题是,当我尝试从引用表中添加数据时,验证不起作用

型号类别:

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; }
}
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)
}
元数据:

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 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; }
}
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)
}
视图:

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; }
}
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_代码进行了测试,student_代码字段工作正常,因为student_代码属性在同一个模型中(ms_student),而不像电子邮件(参考ms_person)

所有的元数据验证工作,比如两个模型都需要,所以我猜错误在JsonResult参数上

非常感谢

只需将其更改为

@Html.TextBoxFor(model => model.ms_person.email, new{@id="email", @name="email"})

更改操作方法以包括
绑定前缀
属性/属性

public JsonResult CheckEmailExist([Bind(Prefix="ms_person.email")]string email)
{
  ...

您可以尝试
public JsonResult CheckEmailExist([Bind(Prefix=“ms_person.email”]string email){..
尝试一下,但不起作用:O,现在正在搜索另一个解决方案..刚刚测试过,它应该是
public JsonResult CheckEmailExist([Bind(Prefix=“ms_person.email”]string email){..
很好,我想您失踪了”)”就在“]”之前,itu应该是JsonResult CheckEmailExist([Bind(Prefix=“ms_person.email”)]string email)Thx:当然(笨拙的手指)。我将作为答案发布。这不会更改名称。Html帮助程序覆盖任何设置
名称
属性`的尝试,只是询问我是否使用新的{}更改名称和id,这是否意味着实体框架将无法识别id和名称,电子邮件也不会插入数据库?只是好奇…还没有尝试。。。