2012-08-16 130 views
-1

我在這裏打擊1000我的問題。所以我會盡量保持描述性。ViewModel來處理多個數據模型

我在來自不同模型的佈局中有多個視圖。 當從列表中選擇一條記錄時,它將打開此佈局。在佈局頂部,它以表格格式顯示記錄信息。這是一個簡單的ID -/AuditSchedule/1122。這是目前的身體。這工作。

在佈局的另一個區域中,我有一個從另一個表中生成的操作鏈接列表(側面菜單)。鏈接,我認爲應該如下但不確定/ AuditSchedule/1122/1。這是通過使用帶路由的Global.asax來完成的。

當然,當你打開這個佈局時,你應該得到所有的上述內容以及佈局的下一個區域的第一條記錄,這是表單。在這種形式下,我需要它從一個問題表中顯示一個問題,並在問題的右側創建一組複選框,我將調用分數。這些分數也在一個稱爲分數的表格中。我所擁有的一切都在數據表中,以便在需要時可以編輯和更改所有內容。

當用戶提交表單時,它將存儲在名爲MainAnswers的另一個表中,auditSchedule的id,mainQuestion和分數字符串。該表是一張空白表,因此它將爲該AuditSchedule的每個主要問題插入一條新記錄。

到目前爲止,我對此沒有任何幫助。如果有人能指出我看到他們看到的這個例子。這會很棒。我不能成爲唯一試圖這樣做的人。不過,我是MVC C#的新手。如果這是Zend和PHP,我不會有任何問題。

我已經使用了代碼第一種方法。我所有的關係都完成了。問題在於實現視圖並將信息保存在正確的表中。

任何人都可以提供幫助將不勝感激。我在這裏掙扎。

更新2012年8月16日3點12分

讓我把寶寶的第一個步驟。

我希望能夠從側面選擇一個菜單項目,並從該部分出現問題列表。這是我的代碼:

@{ Layout = null; } 
@model QQAForm.ViewModels.AuditFormEdit 

<table width="698" border="2" cellpadding="2"> 
<tr> 
<td align="center"><b>Section</b><br />1.0</td> 
<td> 

<br />(Title of Section Goes Here - SubcategoryName - Located in Subcategory Model)<br /> 


<br /> 

(Child Questions Go here - QuestionText - Located in ChildQuestion Model) 

</td> 
<td> 
     (This should be the result of what is written in AuditFormEdit view model - it does not currently work - Nothing shows up) 
     @for (int index = 0; index < Model.ScoreCardCheckBoxHelperList.Count; index++) 
     { 

      @Html.CheckBoxFor(m => m.ScoreCardCheckBoxHelperList[index].Checked) 
      @Html.LabelFor(m => m.ScoreCardCheckBoxHelperList[index], Model.ScoreCardCheckBoxHelperList[index].ScoreName) 
      @Html.HiddenFor(m => m.ScoreCardCheckBoxHelperList[index].ScoreID) 
      @Html.HiddenFor(m => m.ScoreCardCheckBoxHelperList[index].ScoreName) 


     } 

    </td> 
</tr> 
</table> 

這裏是視圖模型我的工作:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using QQAForm.Models; 

namespace QQAForm.ViewModels 
{ 
public class AuditFormEdit 
{ 
    public List<SubcategoryHelper> SubcategoryHelperGet { get; set; } 

    public class SubcategoryHelper : Models.SubCategory 
    { 
     public SubcategoryHelper(Models.SubCategory subCat) 
     { 
      this.SubCategoryID = subCat.SubCategoryID; 
      this.SubcategoryName = subCat.SubcategoryName; 
     } 

    } 


    public Models.MainAnswer ScoreInstance { get; set; } 

    public List<ScoreCardCheckBoxHelper> ScoreCardCheckBoxHelperList { get; set; } 

    public void InitializeScoreCheckBoxHelperList(List<Models.Score> ScoreList) 
    { 
     if (this.ScoreCardCheckBoxHelperList == null) 
      this.ScoreCardCheckBoxHelperList = new List<ScoreCardCheckBoxHelper>(); 

     if (ScoreList != null 
      && this.ScoreInstance != null) 
     { 
      this.ScoreCardCheckBoxHelperList.Clear(); 
      ScoreCardCheckBoxHelper scoreCardCheckBoxHelper; 
      string scoreTypes = 
       string.IsNullOrEmpty(this.ScoreInstance.Score) ? 
       string.Empty : this.ScoreInstance.Score; 
      foreach (Models.Score scoreType in ScoreList) 
      { 
       scoreCardCheckBoxHelper = new ScoreCardCheckBoxHelper(scoreType); 
       if (scoreTypes.Contains(scoreType.ScoreName)) 
        scoreCardCheckBoxHelper.Checked = true; 
       this.ScoreCardCheckBoxHelperList.Add(scoreCardCheckBoxHelper); 
      } 
     } 
    } 


    public void PopulateCheckBoxsToScores() 
    { 
     this.ScoreInstance.Score = string.Empty; 
     var scoreType = this.ScoreCardCheckBoxHelperList.Where(x => x.Checked) 
           .Select<ScoreCardCheckBoxHelper, string>(x => x.ScoreName) 
           .AsEnumerable(); 
     this.ScoreInstance.Score = string.Join(", ", scoreType); 
    } 


    public class ScoreCardCheckBoxHelper : Models.Score 
    { 
     public bool Checked { get; set; } 

     public ScoreCardCheckBoxHelper() : base() { } 

     public ScoreCardCheckBoxHelper(Models.Score score) 
     { 
      this.ScoreID = score.ScoreID; 
      this.ScoreName = score.ScoreName; 
     } 
    } 



} 
} 

這裏是控制器部分:

//get 
    public ActionResult _Forms(int section) 
    { 
     AuditFormEdit viewModel = new AuditFormEdit(); 
     //viewModel.ScoreInstance = _db.MainAnswers.Single(r => r.MainAnswerID == id); 
     _db.SubCategories.Single(r => r.SubCategoryID == section); 
     viewModel.InitializeScoreCheckBoxHelperList(_db.Scores.ToList()); 
     return View(viewModel); 
    } 



    //post 
    [HttpPost] 
    public ActionResult _Forms(AuditFormEdit viewModel) 
    { 
     if (ModelState.IsValid) 
     { 
      viewModel.PopulateCheckBoxsToScores(); 
      _db.Entry(viewModel.ScoreInstance).State = System.Data.EntityState.Modified; 
      _db.SaveChanges(); 
      return RedirectToAction("/"); 
     } 
     else 
     { 
      return View(viewModel); 
     } 
    } 

所以,如果你看一下佈局,在那裏它顯示_Forms節的位置應該改變鏈接/ AuditSchedule/1132/1 - 它不會。以及我目前沒有出現的複選框。

回答

0

...我的複選框目前還沒有出現。

這是因爲它設置在控制器的GET動作viewModel.ScoreInstance行被註釋掉:

//viewModel.ScoreInstance = _db.MainAnswers.Single(r => r.MainAnswerID == id); 

因此viewModel.ScoreInstancenullInitializeScoreCheckBoxHelperList您填寫的ScoreCardCheckBoxHelperList只有當viewModel.ScoreInstancenull

if (this.ScoreCardCheckBoxHelperList == null) 
    this.ScoreCardCheckBoxHelperList = new List<ScoreCardCheckBoxHelper>(); 

if (ScoreList != null 
    && this.ScoreInstance != null) 
{ 
    //... add elements to ScoreCardCheckBoxHelperList 
} 

ScoreCardCheckBoxHelperList =沒有複選框。