1

Scenario :

Viewmodel dienstViewModel contains a 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

The view that contains the partial:

<% 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 %>

When i press the commit button on the end only the first 2 textboxes get validated. How do i make sure that the partial view also gets validated for correct input?

Or are partials only used to show information and not to retrieve information?

Partial view

<%@ 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>

Controller Methods that calls the views

 '
        ' 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 
Stefanvds
  • 5,868
  • 5
  • 48
  • 72
David
  • 4,185
  • 2
  • 27
  • 46
  • it should do be validating it. Have you tried simply passing Model.DeisntAdres rather than converting it in viewdatadictionary ? – neebz Apr 04 '11 at 12:32
  • yes, i tried that but that created a new problem : http://stackoverflow.com/questions/650393/asp-net-mvc-renderpartial-with-null-model-gets-passed-the-wrong-type – David Apr 04 '11 at 12:37

1 Answers1

0

probably you are not parsing your POST result into an object of the AdresViewModel, when the POST action is called.

can you copy the code of your action?

for example: (C#)

public ActionResult Edit(AdresViewModel mod) {

}

Edit:

you did:

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

but it should be:

<% Html.RenderPartial("Adres", Model.DienstAdres, new ViewDataDictionary()); %>
Stefanvds
  • 5,868
  • 5
  • 48
  • 72
  • Added my DienstViewModel who contains the AdresViewModel – David Apr 04 '11 at 14:23
  • so you should add both dienstviewmodel and adresviewmodel in your POST method! – Stefanvds Apr 04 '11 at 15:22
  • This works! but still im not getting validation messages on my partial view. any ideas? – David Apr 05 '11 at 08:03
  • does the `DisplayName` work? and what's with the `New ViewDataDictionary(Model.DienstAdres)` ? why is it in a ViewDataDictionary? is this required in vb.net? – Stefanvds Apr 05 '11 at 09:24
  • The labels are created , the textboxes are created but not the validation messages. the new viewdatadictionary is to fix this problem. http://stackoverflow.com/questions/650393/asp-net-mvc-renderpartial-with-null-model-gets-passed-the-wrong-type – David Apr 05 '11 at 12:15
  • Gues im trying to do something that shouldn't be done , thanks for pointing it out – David Apr 07 '11 at 13:36