2017-05-11 5 views
1

モデルでデータフィールドの検証を使用して入力する電話番号の検証を試みていますが、これを行う方法を検索しました。 (私は、私は、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登録ページの下部にスクリプトを追加するような解決策がいくつか見受けられましたが、それでも機能しません。

解決に向けた任意のヒントは本当にメタデータクラスの

+0

ModelBindersをApplication startメソッドに登録しましたか? – vendettamit

+1

global.asaxのApplication_start()またはStartup.csの 'ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();'にこの行を追加します。 – vendettamit

+0

は、それがMicrosoft.web –

答えて

2

プロパティは実際のモデルクラスと同じタイプである必要はありません役立つだろう。まだ存在しない場合は

あなたのページにjQueryの検証スクリプトを追加し

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; } 
    } 

更新:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script> 

チェックをお使いのコントローラににModelStateが有効である場合には、あなたのメタデータクラスのプロパティを変更してみてください:

public ActionResult InscriptionFormation(INSC_Inscription insc) 
{ 
     if (!ModelState.IsValid) 
      return View(); 
.. 
.. 
} 

あなたのコードをplicatedし、これらは私が検証作業を行うために見つからなかったものです。

また、モデルを検証するために別のビューにナビゲートするのではなく、同じビューで検証します。モデルが有効で、別のページで処理する必要がある場合にのみナビゲートします。これは物事を簡単に保つのに役立ちます。

関連する問題