Asp.net 在使用ViewData(MVC)时遇到一些困难

Asp.net 在使用ViewData(MVC)时遇到一些困难,asp.net,view,controller,tempdata,Asp.net,View,Controller,Tempdata,我正在为学校做一个MVC5项目,我在一个关于项目和任务的模型上。因此,基本上,您可以创建、编辑、更新和删除项目(CRUD),但是,对于每个项目,您可以有不同的任务。因此,项目和任务模型如下所示: [Table("Projectos")] public class ProjectoModel { [Key] public int ProjectoID { get; set; } //ProjectID [Required]

我正在为学校做一个MVC5项目,我在一个关于项目和任务的模型上。因此,基本上,您可以创建、编辑、更新和删除项目(CRUD),但是,对于每个项目,您可以有不同的任务。因此,项目和任务模型如下所示:

 [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,其中包括与任务相关的操作,因为每组任务都属于一个项目

// 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

例如,在Terefas视图中: @ActionLink(“Criar Tarefa”,“CreateTarefas”,new{id=TempData[“idproject”]})

但是,对于CreateTarefas视图,我无法将TempData从视图传递到POST操作,因为我使用的是提交操作。正如您在上面看到的,我检查TempData是否为null,它是否真的为null,我检查它。我希望获得TempData,因此每次创建任务时,ProjectID都会自动关联到任务。我不知道我是否解释得很好,谢谢你的帮助

(附注:我的CreateTarefas视图)

@model CodingsESW.Models.TarefaModel
@{
ViewBag.Title=“Criar Tarefa”;
}
跗关节
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
乌玛塔雷法酒店

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.TarefaNome,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.TarefaNome,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.TarefaNome,“,new{@class=“text danger”}) @LabelFor(model=>model.tarefascripticao,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.tarefascripticao,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.tarefascripticao,“,new{@class=“text danger”}) } @ActionLink(“返回列表”、“Tarefas”、新的{id=TempData[“idproject”]}) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
您不能使用tempdata,因为temdata在视图渲染后过期。那么,我可以使用/做什么来代替它呢?…您可以使用projectID和TarefaModel创建不同的模型(viewmodel),并将该值存储在hiddenfield中,并在mvc Action中接受该viewmodel。我最终为每个项目创建了一个任务。:)基本上,在Get控制器中,我创建了一个模型,并使用tempdata为模型提供了ProjectID,然后我将其隐藏在视图中,它就被创建了!谢谢你,巴德;)
@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")
}