マルチモデル フォームがあり、複数の質問があるので、最初の質問から始めます。リストのビューには、フォームを開くアクション リンクがあります。このように開くには、このリンクが必要です。
/監査スケジュール/監査/1192/セクション/1
現在、リンクはこのようになっています
/監査スケジュール/監査/1192/セクション
アクションリンクのコードは次のとおりです
grid.Column(format: (tracking) => Html.ActionLink("Select", "Audit", new { id = tracking.AuditScheduleID })),
Global.asax にリンクを設定しています
routes.MapRoute(
"AuditSchedule", // Route name
"AuditSchedule/Audit/{id}/Section/{section}", // URL with parameters
new { controller = "AuditSchedule", action = "Audit", id = UrlParameter.Optional, section = UrlParameter.Optional } // Parameter defaults
);
セクションは常に 1 になります。このリンクを表示するにはどうすればよいですか?
質問 2 サイド メニューを選択してフォーム ビューに質問を入力します。また、いくつかのチェックボックス用に作成したビューモデルもあり、選択内容が含まれているテーブルからデータを入力します。AuditSchedule の ID を持つレコードを追加すると、入力されます。これはいい。ただし、選択したものを各質問の回答の表に入れるには、これらの質問のそれぞれが必要です。MainQuestionID に (get set) を追加しましたが、すべてゼロが表示されます。
どの質問に対するものかを認識し、質問の表に挿入できるようにするにはどうすればよいですか?
コードは次のとおりです。
namespace QQAForm.ViewModels
{
public class AuditFormEdit
{
public virtual int MainQuestionID { get; set; }
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;
}
}
}
}
ビューは次のとおりです。
@{ Layout = null; }
@model QQAForm.ViewModels.AuditFormEdit
@Html.DisplayFor(model => model.MainQuestionID)
<br />
@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)
<br />
}
コントローラーは次のとおりです。
//get
public ActionResult _ScoreSection(int id)
{
AuditFormEdit viewModel = new AuditFormEdit();
viewModel.ScoreInstance = _db.MainAnswers.Single(r => r.AuditScheduleID == id);
//_db.SubCategories.Single(r => r.SubCategoryID == Section);
viewModel.InitializeScoreCheckBoxHelperList(_db.Scores.ToList());
return View(viewModel);
}
// get
public ActionResult _Forms(int section)
{
var subCatToDisplay = _db.SubCategories.Single(r => r.SubCategoryID == section);
return View(subCatToDisplay);
}
//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);
}
}
結果のレイアウトビューは次のとおりです。
現在、パーシャルなどを介してこれらすべてをレイアウトに取り込んでいます。これを行うためのより簡単な方法またはよりクリーンな方法がある場合はお知らせください。
ありがとう