C# 数据模型dosen和x27中的数据字段验证;行不通
我只是在尝试验证用户将在我的模型中使用数据字段验证输入的电话号码。我已经搜索了如何进行验证,以下是我目前所拥有的信息(我尝试按照我在msdn上看到的信息进行操作:): 这是我的元数据文件:C# 数据模型dosen和x27中的数据字段验证;行不通,c#,entity-framework,validation,model,asp.net-mvc-5,C#,Entity Framework,Validation,Model,Asp.net Mvc 5,我只是在尝试验证用户将在我的模型中使用数据字段验证输入的电话号码。我已经搜索了如何进行验证,以下是我目前所拥有的信息(我尝试按照我在msdn上看到的信息进行操作:): 这是我的元数据文件: using System; using System.Web.Mvc; using System.Web.DynamicData; using System.ComponentModel.DataAnnotations; namespace InscriptionCentreFormation.Models
using System;
using System.Web.Mvc;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;
namespace InscriptionCentreFormation.Models
{
[MetadataType(typeof(INSC_InscriptionMetadata))]
public partial class INSC_Inscription
{
}
public class INSC_InscriptionMetadata
{
[Display(Name = "Mobile Phone")]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Enter a valid format")] //Vérifie le format du tel
public string TelephoneMobile { get; set; }
[Display(Name = "Home Phone")]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Enter a valid format")] //Vérifie le format du tel
public string TelephoneMaison { get; set; }
}
}
这是生成的模型类:
namespace InscriptionCentreFormation.Models
{
using System;
using System.Collections.Generic;
public partial class INSC_Inscription
{
public int id { get; set; }
public int idOccurenceFormation { get; set; }
public string TelephoneMobile { get; set; }
public string TelephoneMaison { get; set; }
}
}
最后,这里是注册页面的简化版本:
@using System.Web.UI.WebControls
@using InscriptionCentreFormation.Controllers
@using InscriptionCentreFormation.Models
@model INSC_Inscription
@{
ViewBag.Title = "InscriptionFormation";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<br />
@using (Html.BeginForm("InscriptionFormation", "DetailProduit"))
{
<div style="width:800px;">
<span style="float:left"><b>Mobile phone :</b></span>
@Html.TextBoxFor(m => m.TelephoneMobile, new { Style = "float:right;width:400px;" })
@Html.ValidationMessageFor(model => model.TelephoneMobile, "", new { @class = "text-danger" })
<br />
<br />
<span style="float:left"><b>Home phone :</b></span>
@Html.TextBoxFor(m => m.TelephoneMaison, new { Style = "float:right;width:400px;" })
@Html.ValidationMessageFor(model => model.TelephoneMaison, "", new { @class = "text-danger" })
</div>
<input type="submit" class="btn" value="S'inscrire" style="width:200px; text-align:center;"/>
}
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@使用System.Web.UI.WebControls
@使用Centreformation.Controllers
@使用中心变形。模型
@模型INSC_铭文
@{
ViewBag.Title=“铭文形成”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@使用(Html.BeginForm(“碑文格式”、“详细产品”))
{
流动电话:
@TextBoxFor(m=>m.TelephoneMobile,新{Style=“float:right;width:400px;”})
@Html.ValidationMessageFor(model=>model.TelephoneMobile,“,new{@class=“text danger”})
家庭电话:
@TextBoxFor(m=>m.TelephoneMaison,新的{Style=“float:right;width:400px;”})
@Html.ValidationMessageFor(model=>model.TelephoneMaison,“,new{@class=“text danger”})
}
但问题是,当我尝试注册时,两个电话号码都没有验证,而且似乎无法理解原因,我看到了一些解决方案,比如在注册页面的底部添加脚本,但仍然不起作用
任何关于解决方案的提示都会非常有用,元数据类中的属性不必与实际模型类中的属性类型相同。尝试将元数据类属性更改为:
public class INSC_InscriptionMetadata
{
[Display(Name = "Mobile Phone")]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Enter a valid format")] //Vérifie le format du tel
public object TelephoneMobile { get; set; }
[Display(Name = "Home Phone")]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Enter a valid format")] //Vérifie le format du tel
public object TelephoneMaison { get; set; }
}
更新
如果页面中尚未添加JQuery验证脚本,请在页面中添加:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
我已经复制了您的代码,这些是我发现的使验证工作正常进行所缺少的东西
也在同一视图上进行验证,而不是将其导航到其他视图以验证模型。仅当模型有效且应由其他页面处理时才进行导航。这将有助于简化操作。您是否在应用程序启动方法中注册了ModelBinder?请在
ModelBinders.Binders.DefaultBinder=new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder()中添加此行
在global.asax或Startup.cs中的应用程序_start()中。@vendettamit它说Mvc不存在于Microsoft.Web中添加using语句using System.Web.Mvc代码>@vendettamit它已经在那里了
public ActionResult InscriptionFormation(INSC_Inscription insc)
{
if (!ModelState.IsValid)
return View();
..
..
}