C# 我的MVCREATE视图甚至在我可以看到表单以输入插入值之前就尝试插入到我的数据库中
我的创建页面基本上是一个表单,它经过硬编码以匹配作为问题模型一部分的insert语句中的参数。我的代码如下:C# 我的MVCREATE视图甚至在我可以看到表单以输入插入值之前就尝试插入到我的数据库中,c#,html,asp.net-mvc,C#,Html,Asp.net Mvc,我的创建页面基本上是一个表单,它经过硬编码以匹配作为问题模型一部分的insert语句中的参数。我的代码如下: 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 =
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;
}
routes.MapRoute(
"Question", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Question", action = "Index", id = "" } // Parameter defaults
);
我的第一条路线记录如下:
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;
}
routes.MapRoute(
"Question", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Question", action = "Index", id = "" } // Parameter defaults
);
我只希望能够看到我的html表单,输入值,单击submit,页面会相应地创建条目并将其插入我的MCQ_问题表中。非常感谢大家的帮助 您通常如何实现这一点是通过
- 有两个独立的
创建
操作
- 用
装饰一个。在这里,您只需返回带有默认模型的视图[HttpGet]
- 用
装饰另一个。在这里,如果验证通过,则插入数据库并返回[HttpPost]
操作,否则返回带有错误的视图Display
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");
}
公共类CreateViewModel
{
public int Poll_id={get;set;}
//...
}
//视图看起来像(从CreateViewModel继承)
m、 民意测验(id)%>
//控制器
公共操作结果创建()
{
CreateViewModel newCreateView=新建CreateViewModel();
返回视图(newCreateView);
}
[HttpPost]
公共操作结果创建(CreateViewModel newCreateView)
{
//将代码保存到数据库中
返回操作(“索引”);
}
在视图中写入数据后,它将消耗您的数据。刚才在这里看了您的帖子,实际上我使用的是Oracle,这些模型似乎适用于microsoft sql,mysql等。有解决办法吗?这只是另一个Mariusz:),这篇文章是针对MVC1的,正如我所看到的,你们正在研究MVC2或MVC3。你们可以在这个论坛上找到答案。只需输入mvc和oracle,它就可以显示您的问题中的许多主题。