My MVC Create View tries to insert into my db even before I can see the form to input values for the insert
my Create page is basically a form that's hardcoded to match arguments in an insert statement thats part of my QuestionModel. My codes are as follows :
public string Create(int Poll_ID, int User_ID, int QuestionGroup, int QuestionType, string Text, int PartialAnswers, int Max, int QOrder, string DataType)
{
//int Permission = CheckPermissionMethod(Poll_ID, User_ID); //user permission id
int Permission = 3;
string Result; // message shown to user
int MaxQuestion_ID;
OleDbDataReader OleDbDataReader;
OleDbDataReader myOleDbDataReader;
if (Permission == 3)
{
if (QuestionGroup < 3) //MCQ or Participant List Question
{
//get the maximum question id
OleDbDataReader = DBConn("SELECT MAX(Question_ID) \"Question_ID\" FROM MCQ_QUESTIONS");
OleDbDataReader.Read();
MaxQuestion_ID = Convert.ToInt32(OleDbDataReader["Question_ID"]) + 1;
myOleDbConnection.Close();
Result = "Question was added to your poll! ";
}
else
开发者_JAVA技巧 Result = "You are not permitted to create a question.";
return Result;
}
This is part of my QuestionModel under my models.
<% using (Html.BeginForm("Index", "Question", FormMethod.Post)) { %>
<% if (!string.IsNullOrEmpty((string) ViewData["ErrorMessage"])) { %>
Poll_Id: <%= Html.TextBox("Poll_Id")%><br />
User_Id: <%= Html.TextBox("User_Id")%><br />
QuestionGroup: <%= Html.TextBox("QuestionGroup")%><br />
QuestionType: <%= Html.TextBox("QuestionType")%><br />
Text: <%= Html.TextBox("Text")%><br />
PartialAnswers: <%= Html.TextBox("PartialAnswers")%><br />
Max: <%= Html.TextBox("Max")%><br />
QOrder: <%= Html.TextBox("QOrder")%><br />
DataType: <%= Html.TextBox("DataType")%><br />
<input type="submit" value="Submit" />
My Question Controller is as follows :
public ActionResult Create()
{
QuestionModel Question = new QuestionModel();
int Poll_Id = Convert.ToInt32(TempData["Poll_Id"]);
int User_Id = Convert.ToInt32(TempData["User_Id"]);
int QuestionGroup = Convert.ToInt32(TempData["QuestionGroup"]);
int QuestionType = Convert.ToInt32(TempData["QuestionType"]);
string Text = Convert.ToString(TempData["Text"]);
int PartialAnswers = Convert.ToInt32(TempData["PartialAnswers"]);
int Max = Convert.ToInt32(TempData["Max"]);
int QOrder = Convert.ToInt32(TempData["QOrder"]);
string DataType = Convert.ToString(TempData["DataType"]);
Question.Create(Poll_Id, User_Id, QuestionGroup, QuestionType, Text, PartialAnswers, Max, QOrder, DataType);
return View();
}
public ActionResult Submit()
{
TempData["Poll_Id"] = Convert.ToInt32(Request.Form["Poll_Id"]);
TempData["User_Id"] = Convert.ToInt32(Request.Form["User_Id"]);
TempData["QuestionGroup"] = Convert.ToInt32(Request.Form["QuestionGroup"]);
TempData["QuestionType"] = Convert.ToInt32(Request.Form["QuestionType"]);
TempData["Text"] = Request.Form["Text"];
TempData["PartialAnswers"] = Convert.ToInt32(Request.Form["PartialAnswers"]);
TempData["Max"] = Convert.ToInt32(Request.Form["Max"]);
TempData["QOrder"] = Convert.ToInt32(Request.Form["QOrder"]);
TempData["DataType"] = Request.Form["DataType"];
//return RedirectToAction("Create");
ViewData["Poll_Id"] = "Setting the values";
return View();
}
}
My first routes entry is as follows :
routes.MapRoute(
"Question", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Question", action = "Index", id = "" } // Parameter defaults
);
I just want to be able to see my html form, enter values, click on submit, and the page creates and inserts the entry into my MCQ_Questions table accordingly. Any help is much appreciated guys!
How you would normally achieve this is by
- having two separate
Create
actions - decorate one with
[HttpGet]
. In here you just return the view with a default model - decorate the other with
[HttpPost]
. Here you insert to database and returnDisplay
action if validation passes otherwise return view with errors.
I would recomend you to make ViewModel for create as a class.
public class CreateViewModel
{
public int Poll_id = {get ; set;}
//...
}
//View would look like (inherits from CreateViewModel)
<%: Html.TextBoxFor(m => m.Poll_id) %>
//Controller
public ActionResult Create()
{
CreateViewModel newCreateView= new CreateViewModel();
return View(newCreateView);
}
[HttpPost]
public ActionResult Create(CreateViewModel newCreateView)
{
//Put code to save all into database here
return RedirectToAction("Index");
}
It will sumbit your data after writing them in the view.
精彩评论