按ID显示视图中的记录

时间:2012-09-21 18:41:17

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

我有一个包含问题的表,我还有一个表,其答案表中列出了问题ID。为了重复数据,我只使用id。

我的问题是如何根据当前表中的ID在“视图中”填充问题。它们已经被MainQuestionID加入。我首先使用DBEntities代码。

这是我的页面的viewModel。

    public class AuditFormEdit
    {
    public Models.MainQuestion MainQuestionID { get; set; }

    public ICollection<MainQuestion> mainQuestion { 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 MainAnswerInstance { 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.MainAnswerInstance != null)
        {
            this.ScoreCardCheckBoxHelperList.Clear();
            ScoreCardCheckBoxHelper scoreCardCheckBoxHelper;
            string scoreTypes =
                string.IsNullOrEmpty(this.MainAnswerInstance.Score) ?
                string.Empty : this.MainAnswerInstance.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.MainAnswerInstance.Score = string.Empty;
        var scoreType = this.ScoreCardCheckBoxHelperList.Where(x => x.Checked)
                              .Select<ScoreCardCheckBoxHelper, string>(x => x.ScoreName)
                              .AsEnumerable();
        this.MainAnswerInstance.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;
        }
    }



}

您可以看到我正在为MainQuestions调用ICollection,但我不确定如何填充问题。他们还必须根据sectionID进行填充。

修订于2012年9月21日2:51

或者,如果我显示问题列表,我将如何将两者联系起来,然后在回答时会更新答案表而不是当前显示的表格。

0 个答案:

没有答案