[英]How do I refresh my datagrid view from another form after I insert and update my values inside my database?
[英]My MVC Create View tries to insert into my db even before I can see the form to input values for the insert
我的创建页面基本上是一个在插入语句中硬编码以匹配 arguments 的表单,这是我的 QuestionModel 的一部分。 我的代码如下:
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
Result = "You are not permitted to create a question.";
return Result;
}
这是我的模型下的 QuestionModel 的一部分。
<% 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" />
我的问题 Controller 如下:
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();
}
}
我的第一个路线条目如下:
routes.MapRoute(
"Question", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Question", action = "Index", id = "" } // Parameter defaults
);
我只想能够看到我的 html 表单,输入值,单击提交,页面会相应地创建条目并将其插入到我的 MCQ_Questions 表中。 非常感谢任何帮助!
您通常如何实现这一目标是通过
Create
动作[HttpGet]
装饰一个。 在这里,您只需返回带有默认 model 的视图[HttpPost]
装饰另一个。 如果验证通过,则在此处插入数据库并返回Display
操作,否则返回错误的视图。我建议您将 ViewModel 创建为 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");
}
在将它们写入视图后,它将汇总您的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.