簡體   English   中英

如何在asp.net mvc 3中驗證多個模型?

[英]How to validate multiple models in asp.net mvc 3?

我有一個簡單的模型,如:

public class AppointmentModel
   {
      private static List<SampleModel> _list;

      public string ClientName {
         get;
         set;
      }

      [DataType( DataType.Date )]
      public DateTime Date {
         get;
         set;
      }

      public bool TermsAccepted {
         get;
         set;
      }

      public List<SampleModel> SampleModelList {
         get;
         set;
      }

      public static List<SampleModel> GetSampleList() {
         if ( _list == null ) {

            _list = new List<SampleModel>( 0 );

            _list.Add( new SampleModel {
               Id = 1,
               Name = "Test",
               IsChecked = false
            } );

            _list.Add( new SampleModel {
               Id = 2,
               Name = "Another test",
               IsChecked = true
            } );

            _list.Add( new SampleModel {
               Id = 3,
               Name = "All test",
               IsChecked = false
            } );
         }

         return _list;
      }
   }

SampleModel看起來像(它是為了模擬復選框輸入而創建的):

public class SampleModel
   {
      public int Id {
         get;
         set;
      }

      public string Name {
         get;
         set;
      }

      public bool IsChecked {
         get;
         set;
      }
   }

在我看來,我實施了:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SimpleAppWithModelBinding.Models.AppointmentModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Page</h2>

   <% using ( Html.BeginForm(FormMethod.Post) ) { %>
         <%:Html.ValidationSummary()%>

      <p>Name: <%: Html.EditorFor( model => model.ClientName )%></p>
      <p>Date: <%: Html.EditorFor( model => model.Date )%></p>
      <p><%: Html.EditorFor( model => model.TermsAccepted )%> Accept terms</p>

      <%-- here I put the checkbox list using editor template --%>
      <%: Html.Action( "RenderPartialSample" ) %>

      <input type="submit" value="Test" />
   <%} %>

</asp:Content>

和控制器端, HomeController

      public ActionResult Page() {

         return View();
      }

      // explicit validate model
      [HttpPost]
      public ActionResult Page( AppointmentModel model ) {

        // do some verification here

         if ( model.SampleModelList == null || ( model.SampleModelList != null && model.SampleModelList.Count == 0 ) ) {
            ModelState.AddModelError( "SampleModelList", "Please check something !" );
         }

         if ( ModelState.IsValid ) {
            //do something here
            return View( "Completed", model );
         } else {
            return View( "Page", model );
         }
      }


      public PartialViewResult RenderPartialSample() {
         List<SampleModel> model = new List<SampleModel> {
            new SampleModel{
               Id = 1,
               IsChecked = true,
               Name = "Test"
            },

            new SampleModel{
               Id = 2,
               IsChecked = false,
               Name = "Test1"
            },

            new SampleModel{
               Id = 3,
               IsChecked = false,
               Name = "Test2"
            }
         };

         AppointmentModel a = new AppointmentModel();
         a.SampleModelList = model;

         return PartialView( "SamplePartialView", a.SampleModelList );
      }

問題:

當我按下提交時, public ActionResult Page( AppointmentModel model )給了我一個模型,他有SampleModelList null,總是為null。 我希望從模型中獲取該列表中的已檢查輸入,但由於部分視圖可能無法正常工作。

如何在我的情況下驗證兩個模型? 或者在我的情況下最好的方法是什么,也許我的方法並不好。

請幫忙 :)

更新:

SamplePartialView包含:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<SimpleAppWithModelBinding.Models.SampleModel>>" %>

<%: Html.EditorForModel() %>

和模板:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SimpleAppWithModelBinding.Models.SampleModel>" %>

<%: Html.HiddenFor( x => x.Id ) %>
<%: Html.CheckBoxFor( x => x.IsChecked, new {
      value = Model.Id
   } )%>
<%: Html.LabelFor( x => x.IsChecked, Model.Name ) %>

<br />

您尚未顯示SamplePartialView部分,但我懷疑您內部不尊重輸入字段的命名約定。 它們沒有使用SampleModelList作為前綴,以便遵守默認模型綁定器的naming convention

在這個局部內你應該有這樣的輸入字段:

<input type="text" name="SampleModelList[0].Id" value="1" />
<input type="text" name="SampleModelList[0].Name" value="name 1" />

<input type="text" name="SampleModelList[1].Id" value="1" />
<input type="text" name="SampleModelList[1].Name" value="name 1" />

...

查看表單的呈現標記,並確保您已遵守此約定。

為了遵守這個命名約定,您可以設置模板前綴,在子控制器操作內部呈現您的部分:

ViewData.TemplateInfo.HtmlFieldPrefix = "SampleModelList";
return PartialView("SamplePartialView", a.SampleModelList);

或者如果您願意,可以在部分內部:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<List<SimpleAppWithModelBinding.Models.SampleModel>>" 
%>
<% ViewData.TemplateInfo.HtmlFieldPrefix = "SampleModelList"; %>
<%= Html.EditorForModel() %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM