【问题标题】:passing multiple values from view to controller to database将多个值从视图传递到控制器到数据库
【发布时间】:2014-01-21 16:18:36
【问题描述】:

我有一个模型问题

public class Question
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int QuestionID { get; set; }
    public int SystemAnswer { get; set; }
    public int StudentAnswer { get; set; }


}

在我看来

@foreach (string[] item in ViewBag.Questions1 as List<string[]>)
{
    <tr>
        <td>
            @Html.EditorFor(model => model.StudentAnswer)
            @Html.ValidationMessageFor(model => model.StudentAnswer)
        </td>
    </tr>
}

ViewBag.Questions1 是 x 个字符串的列表

控制器

public ActionResult theAnswer(Question question) 
{        
    int blah = question.StudentAnswer;
    ViewBag.test = blah.ToString();
    return View();
}

控制器应该将question.StudentAnswer int 值传递给另一个视图;但是,它只传递question.StudentAnswer 的第一个值。我怎样才能让它通过question.StudentAnswer??的所有值?

【问题讨论】:

  • 这是一个int。它只有一个值。

标签: c# asp.net-mvc


【解决方案1】:

看起来你想一次传递多个问题,所以你的视图中也需要有多个问题,我看不到问题视图的控制器......

事实上,我会摆脱 ViewBag 业务并将其作为视图模型传入,这将使事情变得更简单

例如看这个: MVC 3 Edit data in a IEnumerable Model View

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多