我正在为学校做一个MVC5项目,我正在使用一个关于项目和任务的模型。基本上,您可以创建,编辑,更新和删除项目(CRUD),但是,对于每个项目,您可以拥有不同的任务。所以Project和Task模型是这样的:
[Table("Projectos")]
public class ProjectoModel
{
[Key]
public int ProjectoID { get; set; } //ProjectID
[Required]
[Display(Name="Nome")]
public string ProjectoNome { get; set; } //ProjectName
[Required]
[Display(Name="Descrição")]
public string ProjectoDescricao { get; set; } //ProjectoDescription
[Display(Name="Equipa Responsável")]
public int EquipaResponsavelID { get; set; }
public virtual EquipaModel EquipaResponsavel { get; set; } // ResponsibleTeam
[Display(Name = "Concluído")]
public bool ProjectoConcluido { get; set; } //ProjectOver
}
[Table("Tarefa")]
public class TarefaModel
{
[Key]
public int TarefaID { get; set; } //TaskID
[Required]
[Display(Name = "Nome")]
public string TarefaNome { get; set; } //TaskName
[Required]
[Display(Name = "Descrição")]
public string TarefaDescricao { get; set; } //TaskDetails
[Required]
[Display(Name = "Concluída")]
public bool TarefaConcluida { get; set; } //TaskDone
[ForeignKey("Projecto")]
public int ProjectoAssociadoID { get; set; }
public virtual ProjectoModel Projecto { get; set; } //AssociatedProject
}
然后我有ProjectController,其中包含与任务相关的操作,因为每组任务都属于一个Project
// GET: Projectos/Tarefas/5
public ActionResult Tarefas(int id)
{
var tarefas = db.Tarefas.Where(tarefa => tarefa.ProjectoAssociadoID == id).ToList();
TempData["IDProjecto"]=id;
return View(tarefas);
}
// GET: Projectos/Tarefas/Create
public ActionResult CreateTarefas(int id) {
TempData["IDProjecto"] = id;
return View();
}
// POST
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateTarefas([Bind(Include = "TarefaID,TarefaNome,TarefaDescricao")] TarefaModel tarefaModel)
{
if (ModelState.IsValid)
{
if (TempData["IDProjecto"] != null)
{
tarefaModel.ProjectoAssociadoID = Convert.ToInt32((TempData["IDProjecto"]).ToString());
db.Tarefas.Add(tarefaModel);
db.SaveChanges();
return RedirectToAction("Index");
}
}
return View(tarefaModel);
}
所以传递ProjectID直到创建任务我在控制器和视图之间使用TempData。
例如在Tarefas View中: @ Html.ActionLink(“Criar Tarefa”,“CreateTarefas”,新{id = TempData [“IDProjecto”]})
但是,对于CreateTarefas View,由于我正在使用提交操作,因此我无法将TempData从View传递到POST操作。正如你在上面看到的,我检查TempData是否为空,它确实是,我检查它。我希望获得TempData,因此每次创建Task时,ProjectID都会自动与任务相关联。我不知道自己是否解释得很好,谢谢你的帮助。 ;)
(PS:我的CreateTarefas视图)
@model CodingsESW.Models.TarefaModel
@{
ViewBag.Title = "Criar Tarefa";
}
<h2>Criar Tarefa</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Criar uma Tarefa</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.TarefaNome, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TarefaNome, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TarefaNome, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TarefaDescricao, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TarefaDescricao, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TarefaDescricao, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Tarefas", new { id = TempData["IDProjecto"] })
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}