我正在创建一个系统,用户可以在该系统中创建要完成的新任务,每个任务都有多个步骤。这一切都是通过一个输入表单完成的,顶部的部分用于输入任务详细信息(名称、描述等),下面的部分用于输入每个步骤的名称和描述。可以使用一个按钮添加步骤,该按钮在单击时通过 ajax 创建一组新的控件,允许用户根据需要执行尽可能多的步骤。
当模型被传回时,控制器会正确拾取输入(它们存储在列表中),但是在我的代码中的某个地方我有一个问题,因为不是在我的数据库的步骤表中创建一个新步骤,而是而是创建了重复的任务。所以我最终创建了(1 + 步骤数)任务并且没有步骤。
我希望这对人们有意义。我正在使用 MySQL 和我编写的自定义 MySQL 类来处理存储过程。我对 c#、.NET 框架和 MVC 模型非常陌生,所以我可能在构建我的代码时都错了,并且可能正在以艰难的方式做事,但这里就是这样。
这是我的代码:
控制器:
[HttpPost]
public ActionResult Index(TaskCreateModel i)
{
if (!ModelState.IsValid)
{
ModelState.AddModelError("", "Please fill the required fields. All tasks require at least 1 step.");
return View(i);
}
//write task to the database
var proc = new StoredProcService("create_task");
proc.AddParam(new MySqlParameter("Name", MySqlDbType.VarChar),i.Name);
proc.AddParam(new MySqlParameter("Description", MySqlDbType.VarChar),i.Description);
proc.AddParam(new MySqlParameter("Value", MySqlDbType.Float),i.Value);
proc.DoQuery();
foreach (var item in i.Steps)
{
//add steps
var proc_steps = new StoredProcService("create_task_step");
proc_steps.AddParam(new MySqlParameter("Name", MySqlDbType.VarChar), item.Name);
proc_steps.AddParam(new MySqlParameter("Description", MySqlDbType.VarChar), item.Description);
proc_steps.AddParam(new MySqlParameter("taskID", MySqlDbType.Int32), proc.Results.ElementAt(0));
proc.DoQuery();
}
return View();
}
存储过程服务:
public class StoredProcService
{
private MySqlConnection Connect;
private MySqlCommand CMD;
public List<Object> Results { get; private set; }
public StoredProcService(string name, params MySqlParameter[] args)
{
QuerySetup(name);
foreach (var item in args)
{
AddParam(item);
}
DoQuery();
}
public StoredProcService(string name)
{
QuerySetup(name);
}
//used by all constructors
private void QuerySetup(string name)
{
Connect = new MySqlConnection(ConfigurationManager.ConnectionStrings[<my actual connetion string is here>].ConnectionString);
CMD = new MySqlCommand(name, Connect);
CMD.CommandType = CommandType.StoredProcedure;
}
public void AddParam(MySqlParameter p,object pval)
{
CMD.Parameters.Add(p).Value = pval;
}
public void AddParam(MySqlParameter p)
{
CMD.Parameters.Add(p);
}
public void DoQuery()
{
Results = new List<Object>();
MySqlDataReader myReader;
Connect.Open();
myReader = CMD.ExecuteReader();
int count = 0;
while (myReader.Read())
{
Results.Add(myReader.GetValue(count));
count++;
}
myReader.Close();
Connect.Close();
}
}
使用的型号:
public class TaskCreateModel
{
[Required]
[Display(Name = "Task Name")]
public string Name { get; set; }
[Display(Name = "Task Description")]
public string Description { get; set; }
[Required]
[Display(Name = "Task Value")]
[DisplayFormat(DataFormatString = "${0:F2}")]
public float Value { get; set; }
[Required]
public List<StepCreateModel> Steps { get; set; }
}
public class StepCreateModel
{
[Required]
[Display(Name = "Step Name")]
public string Name { get; set; }
[Required]
[Display(Name = "Description")]
public string Description { get; set; }
}
意见:
@model ExampleProject.Models.TaskCreateModel
@{
ViewBag.Title = "Create Task";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm("index", "CreateTask"))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>
Please enter task details below:
</legend>
@Html.Partial("_CreateTaskPartial")
<input type="submit" value="Create Task" />
</fieldset>
<fieldset>
<legend>
Steps
</legend>
<div id="editorRows">
</div>
@Ajax.ActionLink("Add Step","BlankEditorRow", new AjaxOptions{ InsertionMode= System.Web.Mvc.Ajax.InsertionMode.InsertAfter,
HttpMethod="Get", UpdateTargetId="editorRows"})
@Html.EditorFor(x => x.Steps)
</fieldset>
}
这是上一个视图中调用的部分视图(我没有发布 AJAX 调用的视图,因为我的 ajax 工作正常)
@model ExampleProject.Models.TaskCreateModel
<ul>
<li>
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
</li>
<li>
@Html.LabelFor(m => m.Description)
@Html.TextBoxFor(m => m.Description)
@Html.ValidationMessageFor(m => m.Description)
</li>
<li>
@Html.LabelFor(m => m.Value)
@Html.TextBoxFor(m => m.Value)
@Html.ValidationMessageFor(m => m.Value)
</li>
</ul>
存储过程:
DELIMITER $$
CREATE DEFINER=`<me>`@`localhost` PROCEDURE `create_task`(in name VARCHAR(128), in description VARCHAR(255), in value FLOAT)
BEGIN
INSERT INTO tasks (Name,Description,Value)
VALUES (name,description,value);
SELECT LAST_INSERT_ID() as TaskID;
END
CREATE DEFINER=`<me>`@`localhost` PROCEDURE `create_task_step`(in Name varchar(128), in Description VARCHAR(255), in TaskID INT)
BEGIN
INSERT INTO steps (name,description,taskID)
VALUES (Name, Description, TaskID);
SELECT LAST_INSERT_ID() as StepID;
END