我正在尝试C#MVC,我偶然发现了以下问题。我正在玩一个小项目,这是一种任务管理工具。此时我有2个(用户,任务)模型和相应的控制器和一组视图。 用户控制器有一种方法'创建'这显然是基于从表单收到的数据创建一个新用户。创建用户时,应自动为他创建任务。这是我的用户控制器创建/注册方法:
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "userId,dateCreated,dateRemoved,level,firstName,lastName,login,emailAddress,password")] User user)
{
user.dateCreated = DateTime.Now;
if (ModelState.IsValid)
{
db.User.Add(user);
db.SaveChanges();
return RedirectToAction("Create", "Tasks", new { Id = user.userId });
//return RedirectToAction("Index");
}
return View(user);
}
这是任务控制器的Create Task方法
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "taskId,dateCreated,dateAssigned,userId,assigneeId,taskCategory,taskType,taskPriority,taskStatus,taskName,dateDue,dateClosed,Comments")] Task task, int? Id)
{
task.dateCreated = DateTime.Now;
if (Id != null)
{
task.userId = (int)Id;
task.dateAssigned = DateTime.Now;
task.dateCreated = DateTime.Now;
task.dateDue = DateTime.Now.AddDays(7);
task.taskCategory = taskCategory.assigned;
task.taskType = taskType.System_Induction;
task.assigneeId = 2;
task.taskPriority = taskPriority.Low;
task.taskName = "Iduction to TaskManagementTool";
task.Comments = "Please go through the following training materials:";
task.taskStatus = taskStatus.Assigneed;
if (ModelState.IsValid)
{
db.Tasks.Add(task);
db.SaveChanges();
ViewBag.TaskAssignedNotification = "Hello " + Session["login"].ToString() + " you have new task assigned" ;
switch (Session["level"].ToString())
{
case "Basic":
return RedirectToAction("LoggedAsBasic", "Home");
break;
default:
return RedirectToAction("LoggedAsBasic", "Home");
}
//return RedirectToAction("Index", "Home");
}
}
else
{
task.userId = Int32.Parse(Session["userId"].ToString());
task.taskStatus = taskStatus.Created;
if (Session["level"].ToString() == "Assigner" && task.taskCategory == taskCategory.assigned)
{
task.dateAssigned = DateTime.Now;
}
if (Session["level"].ToString() == "Basic")
task.taskCategory = taskCategory.own;
if (ModelState.IsValid)
{
db.Tasks.Add(task);
db.SaveChanges();
return RedirectToAction("TasksIndex");
}
}
return View(task);
}
在创建新用户时,应将新用户ID传递给任务控制器的Create方法,并且应为新用户创建预定义任务(if if(Id!= null)),而无需使用HTML打开Create视图形成。 Unfortunatley我无法从用户控制器调用任务控制器的Create方法。充其量我最终会创建要填充的任务。你们能帮帮我解决这个问题吗?
答案 0 :(得分:0)
在这种情况下我需要的是使用users / create action方法创建的新任务对象,以便与用户对象一起使用,因此我不需要指向任何其他控制器/ actionresult来创建任务。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "userId,dateCreated,dateRemoved,level,firstName,lastName,login,emailAddress,password")] User user, Task task)
{
user.dateCreated = DateTime.Now;
if (ModelState.IsValid)
{
db.User.Add(user);
db.SaveChanges();
task.userId = 2;
task.dateAssigned = DateTime.Now;
task.dateCreated = DateTime.Now;
task.dateDue = DateTime.Now.AddDays(7);
task.taskCategory = taskCategory.assigned;
task.taskType = taskType.System_Induction;
task.assigneeId = user.userId;
task.taskPriority = taskPriority.Low;
task.taskName = "Iduction to TaskManagementTool";
task.Comments = "Please go through the following training materials:";
task.taskStatus = taskStatus.Assigneed;
if (ModelState.IsValid) {
db.Tasks.Add(task);
db.SaveChanges();
}
return RedirectToAction("Index");
}
return View(user);
}