Using TempData with returnview()

本文关键字:returnview with TempData Using | 更新日期: 2023-09-27 18:11:18

我有两个ActionResults和我试图只是简单地传递一个int id从一个ActionResult到另一个。我试图使用tempdata,但在查看调试器的值后,值为零。我看到的tempdata示例使用了redirecttoaction()。这可以用returnview()来完成吗?

public ActionResult Details(int? id)
    {
         myEMSurvey mySurvey = db.myEMSurveys.Find(id);
        if (mySurvey == null)
        {
            return HttpNotFound();
        }
        SurveyViewModel svm = new SurveyViewModel();
        svm.mySurvey = mySurvey;
        svm.Questions = (from s in db.myEMSurveyQuestions
                         where s.SurveyID == id
                         select s).ToList();
        svm.Options = (from o in db.myEMQuestionOptions
                       where o.SurveyID == id
                       select o).ToList();
        svm.Anwsers = (from a in db.myEMSurveyAnswers
                       where a.SurveyID == id
                       select a).ToList();
        int intid = id.Value;
        TempData["ID"] = intid;
        return View(svm);
    }
    [HttpPost]
    public ActionResult CsvDownload()
    {
        int id = Convert.ToInt32(TempData["ID"]); //value of id=0, TempData["ID"] = 33
        var Anwsers = (from a in db.myEMSurveyAnswers
                   where a.SurveyID == id
                   select a).ToList();
         //id = 0
    }

CsvDownload在详细信息视图:

@using (Html.BeginForm("CsvDownload", "Survey", FormMethod.Post))
{
<div class="text-center">
    <input type="submit" name="button" value="Download" class="btn btn-success" />
</div>
<br />}

Using TempData with returnview()

我的代码中的错误是转换为int32。

我需要这样转换:

int id = (int)(TempData["ID"]);

这就是所谓的"拆箱"。它是object到int的直接强制转换