【问题标题】:Asp MVC partial does not validatedAsp MVC 部分未验证
【发布时间】:2011-07-29 03:28:30
【问题描述】:

场景:

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 

【问题讨论】:

  • 它应该正在验证它。您是否尝试过简单地传递 Model.DeisntAdres 而不是在 viewdatadictionary 中转换它?
  • 是的,我试过了,但这产生了一个新问题:stackoverflow.com/questions/650393/…

标签: asp.net-mvc vb.net validation partials


【解决方案1】:

当调用 POST 操作时,您可能没有将 POST 结果解析为 AdresViewModel 的对象。

你能复制你的动作代码吗?

例如:(C#)

public ActionResult Edit(AdresViewModel mod) {

}

编辑:

你做到了:

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

但应该是:

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

【讨论】:

  • 添加了包含 AdresViewModel 的 DienstViewModel
  • 所以你应该在你的 POST 方法中同时添加 dienstviewmodel 和 adresviewmodel !
  • 这行得通!但我仍然没有在我的部分视图中收到验证消息。有什么想法吗?
  • DisplayName 有效吗? New ViewDataDictionary(Model.DienstAdres) 是怎么回事?为什么它在 ViewDataDictionary 中?这在 vb.net 中是必需的吗?
  • 创建了标签,创建了文本框,但没有创建验证消息。新的 viewdatadictionary 就是为了解决这个问题。 stackoverflow.com/questions/650393/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
  • 2012-12-26
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
相关资源
最近更新 更多