1

シナリオ :

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

最後にコミット ボタンを押すと、最初の 2 つのテキスト ボックスのみが検証されます。部分ビューも正しい入力に対して検証されるようにするにはどうすればよいですか?

それとも、パーシャルは情報を表示するためだけに使用され、情報を取得するためには使用されませんか?

部分図

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

1 に答える 1

0

おそらく、POST アクションが呼び出されたときに、POST の結果を AdresViewModel のオブジェクトに解析していません。

アクションのコードをコピーできますか?

例: (C#)

public ActionResult Edit(AdresViewModel mod) {

}

編集:

あなたがした:

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

ただし、次のようにする必要があります。

<% Html.RenderPartial("Adres", Model.DienstAdres, new ViewDataDictionary()); %>
于 2011-04-04T14:14:28.610 に答える