2011-07-10 49 views
1

我構建流利的助手,用於在ASP MVC中構建html表單。 但客戶驗證工作在這種情況下工作。 請幫我理解爲什麼在我的幫手「EditorFor」不添加客戶端驗證。ASP MVC客戶端驗證和流利的幫手

@using (Html.BeginForm()) 
{ 
    var html = Html.TextBoxFor(c => c.UserName); // Good example client validation 
    <input type="submit" value="ok" /> 
} 

@FormHelpers.Form(Html.Form() 
    .Title("Connection utilisateur") 
    .Error("") 
    .AddMessage("Entrez vos identifiant.") 
    .AddRow(row => row 
     .AddField(
      "Nom utilisateur",new EditItem() 
      { 
       Value = Html.TextBoxFor(model => model.UserName), 
       // Validation client is not added ! 
       Error = Html.ValidationMessageFor(model => model.UserName), 
      } 
      )) 
    .AddRow(row => row 
     .AddField(
      "Mot de passe", 
      Html.Edit(model => model.Password))) 
    .AddRow(row => row 
     .AddField(
      "Se souvenir<br/> de moi", 
      Html.Edit(model => model.Souvenir))) 
    .Build()) 

我的生成器梅索德:

public FormViewModelBuilder AddRow(Func<FormRowViewModelBuilder, FormRowViewModelBuilder> rowBuildFunc) 
     { 
      FormRowViewModelBuilder builderRow = new FormRowViewModelBuilder(); 
      FormRowViewModel row = rowBuildFunc(builderRow).FormRowViewModel; 
      FormViewModel.AddRow(row); 

      return this; 
     } 
public FormRowViewModelBuilder AddField(string title, EditItem value) 
    { 
     FormFieldViewModel info = new FormFieldViewModel(); 
     info.Title = title; 
     info.EditItem = value; **// Validation is not added !** 
     FormRowViewModel.Fields.Add(info); 
     return this; 
    } 

回答

0

我有解決方案,但我不明白MvcForm與生成客戶端驗證交互。 簡單的解決方案:

@using (Html.BeginForm()) 
{ 
@FormHelpers.Form(Html.Form() 
    .Title("Connection utilisateur") 
    .Error("") 
    .AddMessage("Entrez vos identifiant.") 
    .AddRow(row => row 
     .AddField(
      "Nom utilisateur",new EditItem() 
      { 
       Value = Html.TextBoxFor(model => model.UserName), 
       // Validation client is not added ! 
       Error = Html.ValidationMessageFor(model => model.UserName), 
      } 
      )) 
    .AddRow(row => row 
     .AddField(
      "Mot de passe", 
      Html.Edit(model => model.Password))) 
    .AddRow(row => row 
     .AddField(
      "Se souvenir<br/> de moi", 
      Html.Edit(model => model.Souvenir))) 
    .Build()) 
} 

或者

//Contructor 
public FormViewModelBuilder(MvcForm mvcForm) 
{ 
    this.FormViewModel.MvcForm = mvcForm; 
} 
// Builder methode, Must be called in last 
public FormViewModel Build() 
{ 
    this.FormViewModel.MvcForm.EndForm(); 
    return FormViewModel; 
} 
0

是否啓用

<appSettings> 
    <add key="ClientValidationEnabled" value="true"/> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
</appSettings> 

在web.config文件?你有沒有加載MVC JavaScript文件?

<script src="@Url.Content("~/Scripts/jquery.tools.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
+0

是的,我有好的劇本和web.config中configurate。問題是流利的編輯器不在MvcForm塊中。 – neoncyber