C# 将多个(几乎)相同的方法与不同的视图相结合?

C# 将多个(几乎)相同的方法与不同的视图相结合?,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,在我当前的MVC项目中,我有一个采用以下方法的控制器: public ActionResult Index(string answer) { using (S3WEntities1 ent = new S3WEntities1()) { afqList.Question = ent.Questions.Where(w => w.QuQuestionId == 1).Select(s => s.QuQuestion).F

在我当前的MVC项目中,我有一个采用以下方法的控制器:

 public ActionResult Index(string answer)
    {
        using (S3WEntities1 ent = new S3WEntities1())
        {
            afqList.Question = ent.Questions.Where(w => w.QuQuestionId == 1).Select(s => s.QuQuestion).FirstOrDefault().ToString();
            afqList.Answers = ent.Answers.Where(w => w.AnsQuestionId == 1).Select(s => s.AnsAnswer).ToList();
        }

        return View(afqList);
    }
但是,此方法重复了5次,唯一的区别是
(w=>w.qQuestionID==x)
(w=>w.AnsQuestionId==x)
中的数字发生了变化,并且每个方法都有5个不同但仍然相似的视图。我怎样才能使这段代码成为一个大批量的代码,而不是拥有5个几乎相同的方法,但仍然拥有不同的视图?提前谢谢

编辑: 我还应该提到,在每种方法中,相应的视图都有一个

@using (Html.BeginForm("Question3", "ControllerName", "FormMethod.Post))

因此需要调用不同的方法,根据下一个方法和视图中所述的方法调用不同的方法

将数字替换为将作为参数传递的x

 public ActionResult Index(string answer, int x)
    {
        using (S3WEntities1 ent = new S3WEntities1())
        {
            afqList.Question = ent.Questions.Where(w => w.QuQuestionId == x).Select(s => s.QuQuestion).FirstOrDefault().ToString();
            afqList.Answers = ent.Answers.Where(w => w.AnsQuestionId == x).Select(s => s.AnsAnswer).ToList();
        }

        return View(afqList);
    }

将数字替换为将作为参数传递的x

 public ActionResult Index(string answer, int x)
    {
        using (S3WEntities1 ent = new S3WEntities1())
        {
            afqList.Question = ent.Questions.Where(w => w.QuQuestionId == x).Select(s => s.QuQuestion).FirstOrDefault().ToString();
            afqList.Answers = ent.Answers.Where(w => w.AnsQuestionId == x).Select(s => s.AnsAnswer).ToList();
        }

        return View(afqList);
    }

首先添加到您的模型中:

public string NextQuestion { get; set; }
然后,您可以在操作和视图中使用它:

public ActionResult Index(string answer, int questionId)
    {
        using (S3WEntities1 ent = new S3WEntities1())
        {
            afqList.Question = ent.Questions.Where(w => w.QuQuestionId == questionId).Select(s => s.QuQuestion).FirstOrDefault().ToString();
            afqList.Answers = ent.Answers.Where(w => w.AnsQuestionId == questionId).Select(s => s.AnsAnswer).ToList();
        }

        afqList.NextQuestion = string.Format("Question{0}", questionId + 1);

        return View(afqList);
    }
public ActionResult索引(字符串答案,int-questionId)
{
使用(S3WEntities1 ent=新的S3WEntities1())
{
afqList.Question=ent.Questions.Where(w=>w.QuQuestionId==questionId)。选择(s=>s.QuQuestion.FirstOrDefault().ToString();
afqList.Answers=ent.Answers.Where(w=>w.AnsQuestionId==questionId)。选择(s=>s.AnsAnswer.ToList();
}
afqList.NextQuestion=string.Format(“问题{0}”,问题ID+1);
返回视图(afqList);
}
现在在视图中:

@using (Html.BeginForm(afqList.NextQuestion, "ControllerName", "FormMethod.Post))

@使用(Html.BeginForm(afqList.NextQuestion,“ControllerName”,“FormMethod.Post))
首先添加到您的模型中:

public string NextQuestion { get; set; }
然后,您可以在操作和视图中使用它:

public ActionResult Index(string answer, int questionId)
    {
        using (S3WEntities1 ent = new S3WEntities1())
        {
            afqList.Question = ent.Questions.Where(w => w.QuQuestionId == questionId).Select(s => s.QuQuestion).FirstOrDefault().ToString();
            afqList.Answers = ent.Answers.Where(w => w.AnsQuestionId == questionId).Select(s => s.AnsAnswer).ToList();
        }

        afqList.NextQuestion = string.Format("Question{0}", questionId + 1);

        return View(afqList);
    }
public ActionResult索引(字符串答案,int-questionId)
{
使用(S3WEntities1 ent=新的S3WEntities1())
{
afqList.Question=ent.Questions.Where(w=>w.QuQuestionId==questionId)。选择(s=>s.QuQuestion.FirstOrDefault().ToString();
afqList.Answers=ent.Answers.Where(w=>w.AnsQuestionId==questionId)。选择(s=>s.AnsAnswer.ToList();
}
afqList.NextQuestion=string.Format(“问题{0}”,问题ID+1);
返回视图(afqList);
}
现在在视图中:

@using (Html.BeginForm(afqList.NextQuestion, "ControllerName", "FormMethod.Post))

@使用(Html.BeginForm(afqList.NextQuestion,“ControllerName”,“FormMethod.Post))
有趣。。。但是你可以处理视图中的逻辑。。。但像艾美·巴德一样,他认为自己需要不同的观点,但方法相同。所以我有点组合了。@SynerCoder是的,我有。我认为这非常接近我所需要的。问题tho,是否有必要更改下一个调用的方法,以便在视图中调用下一个方法?(我对我的问题的最新编辑可能会更好地解释这一点)在1视图中拥有逻辑仍然更好。。。同一件事不需要x视图…@DannyGoodall更改了我的答案以更好地反映您的问题。。。但是你可以处理视图中的逻辑。。。但像艾美·巴德一样,他认为自己需要不同的观点,但方法相同。所以我有点组合了。@SynerCoder是的,我有。我认为这非常接近我所需要的。问题tho,是否有必要更改下一个调用的方法,以便在视图中调用下一个方法?(我对我的问题的最新编辑可能会更好地解释这一点)在1视图中拥有逻辑仍然更好。。。同一件事不需要x视图…@DannyGoodall更改了我的答案以更好地反映您的问题