我有两个 ActionResults ,我只想简单地从一个 ActionResult 传递一个int id另一个。我尝试使用 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 />}
答案 0 :(得分:0)
我的代码中的错误是转换为int32。
相反,我需要像这样进行转换:
int id = (int)(TempData["ID"]);
这被称为&#34;拆箱&#34;。它是从对象直接转换为int。