ViewModel来处理多个数据模型
我在这里打击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 - 它不会。以及我目前没有出现的复选框。
...我的复选框目前还没有出现。
这是因为它设置在控制器的GET动作viewModel.ScoreInstance
行被注释掉:
//viewModel.ScoreInstance = _db.MainAnswers.Single(r => r.MainAnswerID == id);
因此viewModel.ScoreInstance
是null
和InitializeScoreCheckBoxHelperList
您填写的ScoreCardCheckBoxHelperList
只有当viewModel.ScoreInstance
是不null
:
if (this.ScoreCardCheckBoxHelperList == null)
this.ScoreCardCheckBoxHelperList = new List<ScoreCardCheckBoxHelper>();
if (ScoreList != null
&& this.ScoreInstance != null)
{
//... add elements to ScoreCardCheckBoxHelperList
}
空ScoreCardCheckBoxHelperList
=没有复选框。