繁体   English   中英

ASP.NET MVC处理控制器中动态参数数量的最佳方法

[英]ASP.NET MVC Best way to handle dynamic number of parameters in controller

我有一个考试的视图模型。 每个考试都有一定数量的问题。 这可能是1个问题,也可能是50个问题。 提交后,我需要循环问题并检查答案。 我有一个解决方案,但我觉得这不是一个最好的做法。

int questionNumber = 1;

        while (Request.Form["Question" + questionNumber.ToString()] != null)
        {
            int questionID = Convert.ToInt32(Request.Form["Question" + questionNumber.ToString()]);
            int answerID = Convert.ToInt32(Request.Form["Answer" + questionNumber.ToString()]);

            //TODO: Check if answer is correct
        }

不确定另一种方式来做这件事

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult GradeTest(int? testID, string[] questionIDs, string[] answerIDs)

我正在做的事情感觉有点hacky。 请帮助或让我知道我在正确的轨道上。 谢谢!

我真的没有得到整个上下文,但如果这是从表单中的视图提交,那么表单可能是使用@ Html.TextBoxFor或类似的东西构建的。 只需将相同的模型作为后期操作的输入。 请注意,任何不在表单字段中的属性都不会包含在内,如果您必须拥有某些内容,请使用HiddenFor。 我在下面举了一个例子。

YourViewModel.cs

public class YourViewModel {
    public int ExamID { get; set; }
    public string Name { get; set; }
    public List<int> QuestionIDs { get; set; }
    public List<int> AnswerIDs { get; set; }
}

YourView.cshtml

@model YourViewModel.cs
using(Html.BeginForm("PostExam", "YourController", FormMethod.Post)        
{
    @Html.HiddenFor(m => m.ExamID)
    @Html.AntiForgeryToken()
    <strong>Please enter your name</strong>
    @Html.TextBoxFor(m => m.Name)
    @*Your question and answers goes here*@
    <input type="submit" value="Hand in" />
}

YourController.cs

public class YourController : Controller
{
    [HttpPost]
    [ValidateAntiForgeryToken()]
    public ActionResult PostExam(YourViewModel Submitted)
    {
        //Handle submitted data here
        foreach(var QuestionID in Submitted.QuestionIDs)
        {
            //Do something
        }
    }
}

使用带有列表的viewmodel。 唯一需要注意的是绑定到List是一种先进的技术: 模型绑定到List MVC 4

public class Response {
   public string QuestionId {get;set;}
   public string AnswerId {get;set;}
}

public class ExamViewModel {
    public int? TestId {get;set;}
    public List<Response> Responses {get;set;}
}

public ActionResult GradeTest(ExamViewModel viewModel)
{
...

我是一个叮咚。 我错了。 我查找了如何将视图中的集合传递给控制器​​并解决了问题!

http://www.c-sharpcorner.com/UploadFile/pmfawas/Asp-Net-mvc-how-to-post-a-collection/

我像这样更新了我的视图/控制器:

@foreach (var question in Model.TestQuestions)
{
    @Html.Hidden("Questions[" + questionIndex.ToString() + "].ID", question.ID)
    <h3>@question.Text</h3>
    <section>
        @foreach (var answer in question.TestAnswers)
        {
            <div>
                @Html.RadioButton("Answers[" + questionIndex.ToString() + "].ID", answer.ID) @answer.Text
            </div>
        }
    </section>
    <hr />        
    questionIndex++;    
}

和控制器:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult TestDoGrade(int? testID, IEnumerable<TestQuestion> questions, IEnumerable<TestAnswer> answers)
    {

您可以接受JObject作为参数。 JObject它会将隐藏的表单数据转换为可以枚举的JProperties列表。

JProperty有两个字段,Name和Value。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM