2013-03-18 171 views
0

你好嗎?我試圖在ASP NET MVC中驗證表單。asp net mvc部分視圖驗證

我的問題是,當我提交表單,只有父親視圖的控制得到確認的局部視圖「地址」,我重用一些實體,如公司,個人等,在局部視圖中的人不會。

下面是一些代碼,我希望你能geive我的手

人稱視角

@model Entities.Person 


@using (Html.BeginForm("Create", "Person", FormMethod.Post)) 
{ 

    <table> 
     <tr> 
      <td> 
       @Html.LabelFor(model => model.FirstName) 
       <div class="control"> 
        @Html.TextBoxFor(model => model.FirstName, new { @maxlength = 7, @class = "numeric"}) 
        @Html.ValidationMessageFor(model => model.FirstName) 
       </div> 
       <div class="spacer-short"></div> 
      </td> 
      <td> 
       @Html.LabelFor(model => model.LastName) 
       <div class="control"> 
        @Html.TextBoxFor(model => model.LastName, new { @maxlength = 7, @class = "numeric"}) 
        @Html.ValidationMessageFor(model => model.LastName) 
       </div> 
       <div class="spacer-short"></div> 
      </td> 
     </tr> 
    </table> 
    @{ Html.RenderAction("Index", "Address", new {id = Model.AddressId});} //Renders the Address form part 

    <div class="spacer"></div> 
    <button type="submit" data-button="true" data-button-icon="ui-icon-disk" class="">Create</button> 
    <button type="button" data-button="true" data-button-icon="ui-icon-circle-close" class="button-cancel">Cancel</button> 
} 

地址檢視

@model Entities.Address 


<table> 
    <tr> 
     <td> 
      @Html.LabelFor(model => model.Street) 
      <div class="control"> 
       @Html.TextBox("Address.Street", Model.Street) 
       @Html.ValidationMessage("Address.Street") 
      </div> 
      <div class="spacer-short"></div> 
      </td> 
      <td> 
       @Html.LabelFor(model => model.Number) 
       <div class="control"> 
       @Html.TextBox("Address.Number", Model.Number) 
       @Html.ValidationMessage("Address.Number") 
      </div> 
      <div class="spacer-short"></div> 
      </td> 
     </tr> 
    </table> 

然後,我有一些驗證的元數據([必填] )爲人和地址字段

+0

後您是否嘗試過使用具有的RenderAction相同的結果之前,@ Html.Partial( 「地址」) – 2013-03-18 17:18:43

回答

0

試試這個:

@Html.EditorFor(Model.Street) 
@Html.ValidationMessageFor(Model.Street) 

,而不是這樣的:

@Html.TextBox("Address.Street", Model.Street) 
@Html.ValidationMessage("Address.Street") 
0

嘗試使用的Html.Partial(...)代替Html.RenderAction(...)呈現局部視圖。這可能會抑制驗證。

+0

我試過Html.RenderPartial替換@ {Html.RenderAction ...}。那會和Html一樣。部分不是嗎? – 2013-03-18 17:43:30

7

一個常見的錯誤使用@Html.Partial(...)並期待驗證錯誤顯示不流通的ViewData到部分:

@Html.Partial([ViewName], [EmailAddressObject], ViewData) 
+0

謝謝,幫助。問題是,當你只用兩個第一個參數(viewname和通常是model)調用partial時,它可以正常工作(所以我猜ViewData會隱式傳遞)。但是如果你傳遞了一些自定義的內容(例如一個標誌來改變部分輸出)並且不附加現有的ViewData,你將會失去一些信息。在我的情況下,輸入驗證錯誤類不適用於包含部分驗證錯誤的輸入。 – slawek 2013-06-07 15:14:16

0

模式

重要:管理的NuGet包

= >> JQuery.Validation

= >> Microsoft.JQueryUnobtrusive.Validation

安裝。

1)第1步驟中使用System.ComponentModel.DataAnnotations

;

2)

public int Id { get; set; } 
    [Required(ErrorMessage = "* Kategori adı boş geçilemez.")] 
    [DisplayName("Kategori Adı")] 
    public string CategoryName { get; set; } 
    [Required(ErrorMessage = "* Kategori açıklaması boş geçilemez.")] 
    [DisplayName("Kategori Açıklaması")] 
    public string Description { get; set; } 

3)

if (model != null && ModelState.IsValid) 
       { 
        var categoryCreate = new Categories 
        { 
         //code 
        }; 
        _CategoriesService.Create(categoryCreate); 
       } 

4) 添加視圖模型 @model模型。CategoryViewModel

@Html.TextBoxFor(model => model.CategoryName, new { @class = "form-control input-sm", id = "categoryTitle", @placeholder = "Kategori adını giriniz.", @type= "text", @required=true }) 

@Html.ValidationMessageFor(model => model.CategoryName, "", new { @class = "error-message" }) 

@Html.TextBoxFor(model => model.Description, new { @class = "form-control input-sm", id = "categoryArticle", @placeholder = "Kategori açıklamasını giriniz.", @type = "text", @required = true }) 

@Html.ValidationMessageFor(model => model.Description, "", new { @class = "error-message" })