I am doing an MVC5 Project for school and I'm on a model which is about Projects and Tasks. So basically, you can create, edit, update and delete Projects (CRUD), however, for each Project you can have different Tasks. So the Project and Task model are like this:
[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
}
I then have the ProjectController which includes the actions related to the tasks, since each set of tasks belongs to a 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);
}
So to pass on the ProjectID until the creation of the task I'm using TempData that goes between Controller and Views.
For example in Tarefas View: @Html.ActionLink("Criar Tarefa", "CreateTarefas", new { id = TempData["IDProjecto"] })
However, as for the CreateTarefas View I can't manage to pass on the TempData from the View to the POST Action since I'm using a submit action. As you can see above I check if the TempData is null and it really is, I check it. I would like to get that TempData so each time I create a Task, the ProjectID is automatically associated to the task. I don't know if I explained myself well, thanks for the help. ;)
(PS: My CreateTarefas View)
@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")
}