Asp MVC 部分未验证

发布于 2024-10-29 11:45:41 字数 4481 浏览 1 评论 0原文

场景:

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 

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原野 2024-11-05 11:45:41

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

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

例如:(C#)

public ActionResult Edit(AdresViewModel mod) {

}

编辑:

你做了:

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

但它应该是:

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

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()); %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文