簡體   English   中英

從MVC4中的視圖將2個不同的模型傳遞到控制器的適當方法是什么

[英]Whats the Appropriate Method to pass 2 different Models to the Controller from a View in MVC4

我正在將MVC4與SimpleMembership一起使用。 MVC設置了我的UserProfile表,並且我修改了“注冊視圖”以適應我的布局。 一切工作都很好,直到我確定要包括來自用戶的一些其他信息,這些信息最適合另一個已經存在的Model的上下文。

通常,我將多個模型從我的視圖傳遞到控制器的方法是采用元組。 從歷史上看,這對我非常有用,到目前為止,我從未遇到過任何實際問題。

我的注冊表類似於以下內容:

@model Tuple<MyNamespace.Models.RegisterModel,MyNamespace.Models.MembershipDetail>

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
@Html.AntiForgeryToken()     
    <table style="border-collapse: collapse; border-spacing:0px; border-width:0px; margin: 0px; width:100px; padding: 0 0 0 0px; background-color:#2e2e2e;">
        <tr>
           <td>
              Profile Name
           </td>
           <td>
              @Html.TextBoxFor(m => m.Item2.ProfileName)
           </td>
        </tr> 
        <tr>
           <td>
              @Html.LabelFor(m => m.Item1.UserName)
           </td>
           <td>
              @Html.TextBoxFor(m => m.Item1.UserName)
           </td>
        </tr>
        <tr>
           <td>
              @Html.LabelFor(m => m.Item1.Password)
           </td>
           <td>
              @Html.PasswordFor(m => m.Item1.Password)
           </td>
        </tr>
        <tr>
           <td>
              @Html.LabelFor(m => m.Item1.ConfirmPassword)
           </td>
           <td>
              @Html.PasswordFor(m => m.Item1.ConfirmPassword)
           </td>
        </tr>                               
        <tr>
           <td>
              <button type="submit" id="btnSubmitForm" value="Register">Register</button>
           </td>
        </tr>
     </table>
     @Html.Partial("_ValidationSummary",ViewData.ModelState)                             
   }    

我的控制器的Register方法類似於以下內容:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model, MembershipDetail member)
    {
        if (ModelState.IsValid)
        {
            try
             {
                // Do something with the data
             }
            catch(MembershipCreateUserException e)
            {
               ModelState.AddModelError("",ErrorCodeToString(e.StatusCode));
            }
        }
        return View(model);
    }

單擊“提交”按鈕后,只要調試該代碼,我都會注意到返回到控制器的兩個模型都是空的。 包含的字段為null,0或Empty Strings,我不知道為什么。

為了增加這個奧秘,如果我從視圖頂部刪除元組並將其重新分配為單個Model,如下所示:

@model MyNamespace.Models.RegisterModel

並用1模型推斷的引用(m.Property等)替換代碼中的元組引用(例如,egmItem1.Property,m.Item2.Property); 然后將從TextBox幫助器傳入的數據分配給模型,並在調試代碼時填充模型。

現在,我知道我可以簡單地向UserProfile表中添加一些其他字段,並在我的模型中使用它們來完全緩解元組,但是隨后我在模式中復制數據,這不是理想的解決方案。 並且請記住,盡管此處提供的示例代碼僅包含2nd Model的1個元素,但實際上將大於1。

那么,為什么在使用元組時不填充模型,有沒有更合適的方法來解決這個問題呢? 或者我不能在單個表單提交中混合使用模型數據嗎???

首先,將Tuple用作模型不是一個好習慣,因為很難讀取代碼。 您應該為此編寫頁面自己的視圖模型。 對於這樣的示例:

 public class SomeViewModel{
         public RegisterModel RegisterModel { get; set;}
         public MembershipDetail MembershipDetail {get; set;}
 } 

它易於實現,並且可以根據需要工作。

但是,如果要在視圖上使用元組作為模型。

您應該像

   public ActionResult Register(Tuple<RegisterModel,MembershipDetail> model)
   {
     .... // do something..
   }

因為表單html將像這樣創建:

  <input name="Item1.ProfileName" />  

您會看到,它將嘗試發送默認模型綁定程序不支持的Item1.Profile名稱,以將其轉換為您自己的類。.它期望使用元組或類似:的類。

   public class TheClass {
    public RegisterModel Item1 {get; set;}
    public MemberhipDetail Item2 {get; set;}
 }

基本上就像viewmodel

暫無
暫無

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

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