从模型到另一个模型的默认值

时间:2014-02-13 14:55:58

标签: c# asp.net-mvc

我有一个模型活动,其中包含另一个模型的集合:Exigence。当我显示活动详细信息,包括其Exigences列表时,我需要在此列表中添加另一项操作。问题是我不知道如何将FK ActivityId自动添加到我的新对象Exigence中。在我的数据库中创建时,我的IdActivity为null。

我的模特:

public partial class Activity
{
    public Activity()
    {
        this.EXIGENCE = new HashSet<Exigence>();
    }

    public string IdActivity { get; set; }
    public System.DateTime DateService { get; set; }
    public string Detail { get; set; 
    public virtual ICollection<Exigence> REE_EXIGENCE { get; set; }
}

public partial class Exigence
{
    public string IdExigence { get; set; }
    public System.DateTime BeginingDate { get; set; }
    public Nullable<System.DateTime> FinishDate { get; set; }
    public string Description { get; set; }
    public string IdActivite { get; set; }
    public virtual Activite REE_ACTIVITE { get; set; }

}

创造紧急情况的职能:

 public void AddExigence(Exigence exigence)
    {
        exigence = new Exigence
        {
            IdExigence = InsertNewIdExigence(exigence),
            BeginingDate = DateTime.Today,
            FinishDate = DateTime.Today,
            Description = exigence.Description,
            IdActivite = exigence.IdActivite
        };

        _context.EXIGENCE.Add(exigence);
        _context.SaveChanges();
    }

我的控制器:

[HttpPost]
    public ActionResult CreateExigence(Exigence exigence)
    {
        if (ModelState.IsValid)
        {
            _serviceExigence.AjouterExigence(exigence);
            return RedirectToAction("ShowExigence", new { id = exigence.IdExigence});
        }

        Activity act = new Activity();
        ViewBag.IdActivity = _serviceActivity.FindIdActivity(act.IdActivite);
        return View(exigence);
    }

0 个答案:

没有答案