在从一个动作转移到另一个动作期间丢失数据

时间:2016-05-09 12:54:33

标签: c# asp.net-mvc

我在从一个动作转移到另一个动作期间丢失了数据

怎么了?我这样做:

public ActionResult Index(CV model)
    {
        return View();
    }

    public ActionResult rr()
    {
        CV _cv = new CV();
        _cv.education = new List<Education>();
        _cv.education.Add(new Education()
        {
            Faculty = "sa",
            OnGoing = false,
            Specialization = "asdasd",
            UniversityName = "sulxan",
            EndDate = DateTime.Now.AddDays(1),
            StartDate = DateTime.Now

        });
        return RedirectToAction("Index", _cv);
    }

当我调试索引参数model.education.count = 0而不是1.在rr动作中,它是1,具有所需的值。

我的模特课是:

public class CV
    {

        public List<Education> education { get; set; }
        public Education newEducation { get; set; }
    }

public class Education
    {
        public string UniversityName { get; set; }
        public string Faculty { get; set; }
        public string Specialization { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public bool OnGoing { get; set; }
    }

3 个答案:

答案 0 :(得分:1)

发布答案,因为我太多的菜鸟要发表评论。

Stephen Muecke在评论中说的完全正确 - 而且,坚持你的数据绝对重要。另外需要注意的是,根据您发布的代码,如果您要做的只是返回具有所需视图的模型,则不需要RedirectToAction:

  

返回视图(&#34;索引&#34;,_ cv);

当然,如果不了解应用程序的其余部分是如何构建的,那么可能会导致问题。

答案 1 :(得分:0)

您可以使用tempdata存储实体并检索数据。请使用此代码

public ActionResult Index()
{
    CV model = (CV)TempData["cv"];
    return View();
}

public ActionResult rr()
{
    CV _cv = new CV();
    _cv.education = new List<Education>();
    _cv.education.Add(new Education()
    {
        Faculty = "sa",
        OnGoing = false,
        Specialization = "asdasd",
        UniversityName = "sulxan",
        EndDate = DateTime.Now.AddDays(1),
        StartDate = DateTime.Now

    });
    TempData["cv"] = _cv;
    return RedirectToAction("Index");
}

答案 2 :(得分:0)

您可以使用tempdata 像这样

public ActionResult Index()
{
    var model = TempData["CV "] as CV;
    return View();
}

public ActionResult rr()
{
    CV _cv = new CV();
    _cv.education = new List<Education>();
    _cv.education.Add(new Education()
    {
        Faculty = "sa",
        OnGoing = false,
        Specialization = "asdasd",
        UniversityName = "sulxan",
        EndDate = DateTime.Now.AddDays(1),
        StartDate = DateTime.Now

    });
    TempData["CV"] = _cv;
    return RedirectToAction("Index");
}