2011-12-19 109 views
2

我有三個類將數據保存在MVC提交3

public partial class User 
{   
    public string No_ { get; set; }  
    public string LastName { get; set; } 
    public virtual ICollection<Login> Logins { get; set; } 
    public virtual ICollection<Education> Educations { get; set; } 
} 
public partial class Education 
{ 
    public string No_ { get; set; } 
    public string UserId { get; set; } 
    public string Degree { get; set; } 
    public string Institution { get; set; } 
    public string Percentage { get; set; } 
} 
public partial class Login 
{   
    public string No_ { get; set; }  
    public string UserId { get; set; }  
    public string Username { get; set; } 
    public string Password { get; set; } 
    public virtual User User { get; set; } 
} 

我有三個不同勢模型進行了三次的局部視圖,並使它呈現到一個頁面,如下

@{ 
ViewBag.Title = "Register"; 
Layout = "~/Views/Shared/BlankLayout.cshtml"; 
} 
<h2> 
Register</h2>@using (@Html.BeginForm()) 
{ 
     @Html.Partial("LoginPartialView") 

     @Html.Partial("UserPartialView") 

     @Html.Partial("ProfessionPartialView") 

     <section> 
      <div> 
      <button class="reset">Reset</button> 
      <button class="submit" name="submit" value="Submit">Submit</button> 
      </div> 
    </section> 
} 

我想要的是當我點擊提交按鈕時,部分視圖的所有數據都應該到達[httppost],我可以將數據保存到用戶,教育,登錄表。如何獲取數據到一具有HTTP POST控制器,如控制器:

[HttpPost] 
    public ActionResult Register(?,?,?) 
    { 
     context.Logins.Add(LoginObject); 
     context.Educations.Add(EducationObject); 
     context.Professions.Add(ProfessionObject); 
     return View(); 
    } 

我只是想知道如何獲得上述局部視圖中的數據到httppost控制器使上述

提到我可以保存數據

我幾乎是新手在Mvc 3對不起,如果我沒有任何意義,當我問。請引導我一起

+0

您在每個部分視圖中使用哪些模型? – Eranga 2011-12-19 04:37:52

+0

@Eranga我用他們每個人的登錄,職業,教育模型......用於局部視圖 – Joy 2011-12-19 06:55:14

回答

3

而不是部分我會推薦你​​使用編輯器模板。這裏有一個例子,你如何可以寫一個形式,將保存用戶對象:

@model User 
@{ 
    ViewBag.Title = "Register"; 
    Layout = "~/Views/Shared/BlankLayout.cshtml"; 
} 
<h2> 
Register</h2> 
@using (@Html.BeginForm()) 
{ 
    @Html.EditorFor(x => x.No_) 
    @Html.EditorFor(x => x.LastName) 

    @Html.EditorFor(x => x.Logins) 
    @Html.EditorFor(x => x.Educations) 

    <section> 
     <div> 
      <button class="reset">Reset</button> 
      <button class="submit" name="submit" value="Submit">Submit</button> 
     </div> 
    </section> 
} 

,然後2個相應的編輯器模板:

~/Views/Shared/EditorTemplates/Login.cshtml

@model Login 
... some input fields for the login 

~/Views/Shared/EditorTemplates/Education.cshtml

@model Education 
... some input fields for the education 

和控制器動作:

[HttpPost] 
public ActionResult Register(User model) 
{ 
    // the model object will be correctly populated from the default model binder 
    // here we can save it 
    return View(model); 
} 
+0

這是一個很好的解決方案。但可以想要問一個小事.1st是用戶(你創建的)是一個ViewModel?第二,我們如何在飛行中爲登錄,教育,用戶的編輯器模板聲明運行時腳手架?是否有任何出路,以便我們可以在飛行中打開和關閉模型屬性的腳手架選項,同時查看不同頁面中的編輯選項? – Joy 2011-12-19 08:22:02

+0

@Joy,哦,對不起,我從來沒有使用腳手架,也不知道它是如何工作的。我更喜歡定義自己的視圖模型和視圖。 – 2011-12-19 08:23:51

+0

NP,我會檢查出來然後恢復。雖然我猜viewmodel可能暫時解決我的問題。但我真的在尋找腳手架是否能解決我的問題。而不是創造我自己的觀點。可能在那裏應該存在一個選項,如果我們可以做到這一點,所以我們可以使用ViewModel + EditorTemplates = On Flight Reusable Templates for custom Models :) – Joy 2011-12-19 10:43:42