将值从视图传递到MVC4中的控制器

时间:2014-12-27 15:39:02

标签: asp.net-mvc asp.net-mvc-4

我正在尝试将这些文本框值传递给控制器​​:

@model DTOs.QuestionDTO

@{
    ViewBag.Title = "AddQuestion";
}

<h2>AddQuestion</h2>

@using (Html.BeginForm("AddQuestionDB", "Exam"))
{
    <fieldset>
        <legend>Add Question</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.QuestionDes)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.QuestionDes , new { @id="question" , @name="question"})
            @Html.ValidationMessageFor(model => model.QuestionDes)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Answer1)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Answer1 , new { @id="a1" , @name="a1"})
            @Html.ValidationMessageFor(model => model.Answer1)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Answer2)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Answer2 , new { @id="a2" , @name="a2"})
            @Html.ValidationMessageFor(model => model.Answer2)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Answer3)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Answer3 , new { @id="a3" , @name="a3"})
            @Html.ValidationMessageFor(model => model.Answer3)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Answer4)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Answer4 , new { @id="a4" , @name="a4"})
            @Html.ValidationMessageFor(model => model.Answer4)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Correct)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Correct , new { @id="correct" , @name="correct"})
            @Html.ValidationMessageFor(model => model.Correct)
        </div>

        <p>
            <input type="submit" value="Add" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to Login", "Login")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

这是我的控制器方法的样子:

public ActionResult AddQuestionDB(string question, string a1, string a2, string a3, string a4, string correct)
        {
            ViewBag.a1 = a1;
            ViewBag.a2 = a2;
            ViewBag.a3 = a3;
            ViewBag.a4 = a4;
            ViewBag.correct = correct;
            return View();
        }

我创建了一个View来显示这个Viewbag变量......但是这些变量不会出现。 似乎这些变量都是空的。

@{
    ViewBag.Title = "AddQuestionDB";
}

<h2>AddQuestionDB</h2>

<p>@Session["q"]</p>
<p>@ViewBag.question</p>
<p>@ViewBag.a1</p>
<p>@ViewBag.a2</p>
<p>@ViewBag.a3</p>
<p>@ViewBag.a4</p>
<p>@ViewBag.correct</p>

这就是我的DTO的样子:

namespace DTOs
{
    public class QuestionDTO
    {
        public int ID { get; set; }
        public string QuestionDes { get; set; }
        public string Answer1 { get; set; }
        public string Answer2 { get; set; }
        public string Answer3 { get; set; }
        public string Answer4 { get; set; }
        public int Correct { get; set; }

    }
}

你能解释一下,我该怎么做? 谢谢!!!

2 个答案:

答案 0 :(得分:2)

而不是 -

public ActionResult AddQuestionDB(string question, string a1, string a2, string a3, string a4, string correct)
{
    // Your custom code
     return View();
}

有这个代码 -

 public ActionResult AddQuestionDB(QuestionDTO question)
 {
      // Use 'question' object here to get posted values.
      return View();
 }

我在我本地的一个小示例项目中复制了你的场景,这是我用断点调试代码时的结果 -

enter image description here

答案 1 :(得分:1)

Html.EditorFor does not support setting attributes,这就是您的原始代码无效的原因。 name属性与控制器操作中的参数名称不匹配,因此为其分配了null

您可以使用@ramiramilu's answer,也可以使用TextBoxFor代替:

@Html.TextBoxFor(model => model.Correct , new { id="correct" , Name="correct"})

(请注意,Name必须大写才能使其正常工作。)

示例https://dotnetfiddle.net/ZfzCaZ