我有一个多模型表单并且有多个问题,所以我将从第一个问题开始:在我的列表视图中,我有一个打开表单的操作链接。我需要这个链接像这样打开。
/AuditSchedule/Audit/1192/Section/1
目前链接是这样的
/AuditSchedule/Audit/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 添加了一个(设置),但它显示全为零。
我如何让它填充,以便它知道它是针对哪个问题并将其插入问题表中?
这是代码:
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);
}
}
这是结果布局视图:
我目前已经通过部分等将所有这些拉入布局中。如果有更简单的方法或更清洁的方法可以让我知道。
谢谢