私はここで質問をして 1000 を打っています。だから私はできるだけ説明しようとします。
異なるモデルからの複数のビューがレイアウトに含まれています。リストからレコードを選択すると、このレイアウトが開きます。レイアウトの上部には、レコード情報が表形式で表示されます。これは単純な ID - /AuditSchedule/1122 です。これは現在、本体です。これは機能します。
レイアウトの別の領域には、別のテーブルから生成されたアクション リンク (サイド メニュー) のリストがあります。リンクは次のようになるはずですが、/AuditSchedule/1122/1. これは、ルートで Global.asax を使用することによって実現されました。
当然、このレイアウトを開くと、上記のすべてに加えて、フォームであるレイアウトの次の領域の最初のレコードが取得されます。このフォームでは、質問の表から質問を表示し、質問の右側にスコアと呼ばれるチェックボックスのセットを作成する必要があります。これらのスコアは、スコアと呼ばれるテーブルにもあります。私がこれに持っているものはすべてデータテーブルにあるので、必要に応じてすべてを編集および変更できます。
ユーザーがフォームを送信すると、MainAnswers という別のテーブルに auditSchedule の ID、mainQuestion、およびスコアの文字列が格納されます。このテーブルは空のテーブルであるため、その AuditSchedule のメインの質問ごとに新しいレコードが挿入されます。
これまでのところ、私はこれについて助けてくれませんでした。誰かが私に、彼らが見たこの例を教えてくれたら. それは素晴らしいことだ。これをやろうとしたのは私だけではありません。ただし、MVC C# は初めてです。これが Zend と PHP の場合、問題はありません。
コードファーストのアプローチを使用しました。私の関係はすべて完了しました。問題は、ビューを実装し、正しいテーブルに情報を保存することにあります。
助けてくれる人なら誰でも大歓迎です。私はここで苦労しています。
2012/08/16 15: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 で変更する必要がありますが、そうではありません。現在表示されていないチェックボックスも同様です。