Reputation: 375
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!!!
Upvotes: 0
Views: 1612
Reputation: 126042
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
Upvotes: 1
Reputation: 17182
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 -
Upvotes: 2