モデルでデータフィールドの検証を使用して入力する電話番号の検証を試みていますが、これを行う方法を検索しました。 (私は、私は、MSDNで見た情報を追跡しようとした:https://msdn.microsoft.com/en-us/library/cc488527.aspx):データモデルでのデータフィールドの検証
これは私のメタデータファイルです:
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>
しかし、問題は、私は何の検証を登録しないようにしようとすると、電話番号のいずれかで発生し、ちょうどその理由を把握するように見えることができないということである、I登録ページの下部にスクリプトを追加するような解決策がいくつか見受けられましたが、それでも機能しません。
解決に向けた任意のヒントは本当にメタデータクラスの
ModelBindersをApplication startメソッドに登録しましたか? – vendettamit
global.asaxのApplication_start()またはStartup.csの 'ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();'にこの行を追加します。 – vendettamit
は、それがMicrosoft.web –