0

I'm trying to pass these textbox values to the controller:

@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")
}

this is how my controller method looks like:

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();
        }

And i have created a View to display this Viewbag variables... but these variables wont come up. it seem to be that these variables are null..

@{
    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>

this is how my DTO looks like:

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; }

    }
}

Can you please explain, how i should do this?? thank you!!!

tereško
  • 58,060
  • 25
  • 98
  • 150
Dayan
  • 369
  • 1
  • 5
  • 12
  • Can't tell, but don't see a form. Need a form and if you're using default routing, SomeController with [HttpPost] method SomeMethod would route as /Some/SomeMethod. The parameter would most likely be DTOs.QuestionDTO objectNameGoesHere as it looks like you're using html helpers so you should have proper binding. – Robert Dec 27 '14 at 15:45
  • ok thanks i'll have a look – Dayan Dec 27 '14 at 15:46

2 Answers2

2

Instead of -

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

have this code -

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

I replicated your scenario in a small sample project on my local, here is the outcome when I debugged the code with breakpoint -

enter image description here

ramiramilu
  • 17,044
  • 6
  • 49
  • 66
  • This is how it should be done, though you should also pass the `question` model back to the view to display its values (unlike with ViewBag): `return View(question);` – devqon Dec 30 '14 at 11:14
1

Html.EditorFor does not support setting attributes, which is why your original code wasn't working. The name attribute didn't match the parameter names in your controller action, so they were assigned null.

You can either use @ramiramilu's answer, or you could use TextBoxFor instead:

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

(Note that Name must be capitalized in order for this to work).

Example: https://dotnetfiddle.net/ZfzCaZ

Community
  • 1
  • 1
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307