MVC3多模型表格

时间:2012-08-21 14:47:11

标签: c# asp.net-mvc-3 entity-framework

我有一个多模型表单并且有多个问题所以我将从第一个问题开始:  在我的列表视图中,我有一个打开表单的操作链接。我需要这个链接像这样打开。

/ AuditSchedule /审计/ 1192 /组/ 1

目前链接就像这样

/ AuditSchedule /审计/ 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);
        }
    }

这是结果布局视图: enter image description here

如果有更简单的方法或更干净的方式让我知道,我目前已将所有这些都通过局部等方式拉入布局。

谢谢

0 个答案:

没有答案