2011-04-04 72 views
1

場景:天冬氨酸MVC局部不驗證

視圖模型dienstViewModel包含AdresViewModel

Public Class AdresViewModel 
     <Required(ErrorMessage:="Gelieve een straatnaam op te geven")> 
    <DisplayName("Straat:")> 
    Property Straat As String 

<Required(ErrorMessage:="Gelieve een huisnummer op te geven")> 
<DisplayName("Huisnummer:")> 
Property HuisNummer As String 

<Required(ErrorMessage:="Gelieve een gemeente op te geven")> 
<DisplayName("Gemeente:")> 
<RegularExpression("\b[a-zA-Z0-9._%+-]+,\s[0-9]{4}", ErrorMessage:="Selecteer de correcte gemeente")> 
Property Gemeente As String 

    <DisplayName("Bus")> 
    Property Bus As Integer 

End Class 

包含局部的視圖:

<% Using Html.BeginForm()%> 
     <%: Html.ValidationSummary(True) %> 
     <fieldset> 
     <legend>Vervolledig het onderstaand formulier:</legend> 

     <div class="editor-label"> 
      <%: Html.LabelFor(Function(model) model.DienstNaam) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(Function(model) model.DienstNaam) %> 
      <%: Html.ValidationMessageFor(Function(model) model.DienstNaam) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(Function(model) model.DienstOmschrijving) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(Function(model) model.DienstOmschrijving) %> 
      <%: Html.ValidationMessageFor(Function(model) model.DienstOmschrijving) %> 
     </div> 


    </fieldset> 
<fieldset> 
     <legend>Adres gegevens</legend> 
     <% Html.RenderPartial("Adres", New ViewDataDictionary(Model.DienstAdres))%> 
     </fieldset><p> 
     <input type="submit" value="Create" /> 
    </p> 

<% End Using %> 

當我按下提交的結束按鈕只有前兩個文本框被驗證。 我如何確保部分視圖也能得到正確的輸入驗證?

或者是僅用於顯示信息和不檢索信息的偏好?

局部視圖

<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of Anip.WebGUI.ViewModels.AdresViewModel)" %> 

<%-- The following line works around an ASP.NET compiler warning --%> 
    <%: ""%> 



      <div class="editor-label"> 
       <%: Html.LabelFor(Function(model) model.Straat)%> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBoxFor(Function(model) model.Straat)%> 
       <%: Html.ValidationMessageFor(Function(model) model.Straat)%> 
      </div> 

      <div class="editor-label"> 
       <%: Html.LabelFor(Function(model) model.HuisNummer)%> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBoxFor(Function(model) model.HuisNummer)%> 
       <%: Html.ValidationMessageFor(Function(model) model.HuisNummer)%> 
      </div> 

      <div class="editor-label"> 
       <%: Html.LabelFor(Function(model) model.Bus)%> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBoxFor(Function(model) model.Bus)%> 
       <%: Html.ValidationMessageFor(Function(model) model.Bus)%> 
      </div> 

      <div class="editor-label"> 
       <%: Html.LabelFor(Function(model) model.Gemeente)%> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBoxFor(Function(model) model.Gemeente)%> 
       <%: Html.ValidationMessageFor(Function(model) model.Gemeente)%> 
      </div> 

控制器方法調用的意見

' 
     ' GET: /Dienst/Create 

     Function Create() As ActionResult 
      Return View(New DienstViewModel()) 
     End Function 

     ' 
     ' POST: /Dienst/Create 

     <HttpPost()> _ 
     Function Create(ByVal viewModel As DienstViewModel) As ActionResult 
      If ModelState.IsValid Then 
       Try 
        ' TODO: Add insert logic here 
        Return RedirectToAction("Index") 
       Catch 
        Return View(viewModel) 
       End Try 
      Else 
       Return View(viewModel) 
      End If 
+0

它應該做的是驗證它。您是否嘗試過簡單地傳遞Model.DeisntAdres,而不是在viewdatadictionary中轉換它? – neebz 2011-04-04 12:32:32

+0

是的,我試過,但是這造成了一個新問題:http://stackoverflow.com/questions/650393/asp-net-mvc-renderpartial-with-null-model-gets-passed-the-wrong-type – David 2011-04-04 12:37:54

回答

0

可能是你沒有你的分析結果POST到AdresViewModel的對象,當POST操作被調用。

你能複製你的動作代碼嗎?

例如:(C#)

public ActionResult Edit(AdresViewModel mod) { 

} 

編輯:

你做:

<% Html.RenderPartial("Adres", New ViewDataDictionary(Model.DienstAdres))%> 

,但它應該是:

<% Html.RenderPartial("Adres", Model.DienstAdres, new ViewDataDictionary()); %> 
+0

添加我的DienstViewModel包含AdresViewModel – David 2011-04-04 14:23:01

+0

,因此您應該在您的POST方法中添加dienstviewmodel和adresviewmodel! – Stefanvds 2011-04-04 15:22:01

+0

This Works!但仍然沒有得到我的部分觀點的驗證信息。有任何想法嗎? – David 2011-04-05 08:03:19

相關問題