如何在视图中检索tempdata值? [英] how to retrieve tempdata values in a view?

查看:77
本文介绍了如何在视图中检索tempdata值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控制器中的代码:



公共ActionResult索引(FormCollection表单,员工员工)

{



BusinessLogic logic = new BusinessLogic();

employee.Name = form [Name];

employee.Gender = form [性别];

employee.City = form [City];

employee.Dateofjoining = Convert.ToDateTime(form [Dateofjoining]);

TempData [EmployeeDetails] =员工;



logic.AddEmployee(员工);



返回View();

}

这里我创建了另一个操作方法,将上述tempdata值传递给review操作方法以及如何检索值使用tempdata。



code in controller:

public ActionResult Index(FormCollection form,Employee employee)
{

BusinessLogic logic = new BusinessLogic();
employee.Name = form["Name"];
employee.Gender = form["Gender"];
employee.City = form["City"];
employee.Dateofjoining = Convert.ToDateTime(form["Dateofjoining"]);
TempData["EmployeeDetails"] = employee;

logic.AddEmployee(employee);

return View();
}
and here i have create another action method to pass the above tempdata values to the review action method and how to retrieve the values using tempdata.

public ActionResult review()
       {

       }



和vie对于指数行动方法的w是:



@model BusinessLayer.Employee



@ {

ViewBag.Title =索引;

}



索引





@using(Html.BeginForm()){

@ Html.AntiForgeryToken()

@ Html.ValidationSummary(true)





员工




and view for Index action method is :

@model BusinessLayer.Employee

@{
ViewBag.Title = "Index";
}

Index



@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)


Employee



@ Html.LabelFor(model => model.Name)


@Html.LabelFor(model => model.Name)






@ Html.EditorFor(model => ; model.Name)

@ Html.ValidationMessageFor(model => model.Name)


@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)









@ Html.LabelFor(model => model.Gender)


@Html.LabelFor(model => model.Gender)






@ Html.EditorFor(model => model.Gender)

@ Html.ValidationMessageFor(model => model.Gender)


@Html.EditorFor(model => model.Gender)
@Html.ValidationMessageFor(model => model.Gender)









@ Html.LabelFor(model => model.City)


@Html.LabelFor(model => model.City)






@ Html.EditorFor(model => model.City)

@ Html.ValidationMessageFor(model => model.City)


@Html.EditorFor(model => model.City)
@Html.ValidationMessageFor(model => model.City)









@ Html.LabelFor(model => model.Dateofjoining)


@Html.LabelFor(model => model.Dateofjoining)






@ Html.EditorFor(model => ; model.Dateofjoining)

@ Html.ValidationMessageFor(model => model.Dateofjoining)


@Html.EditorFor(model => model.Dateofjoining)
@Html.ValidationMessageFor(model => model.Dateofjoining)



< input type =submitvalue =创建/>


<input type="submit" value="Create" />



@ Html.ActionLink(返回列表,索引 )


@Html.ActionLink("Back to List", "Index")

推荐答案

阅读本文:什么是ViewData,ViewBag和TempData? - 用于在当前和后续请求之间传递数据的MVC选项 [ ^ ]



您应该考虑的是,TempData的生命周期非常短,不会在请求之间传递。它用于其他东西...

另一方面,为什么你在TempData中传递模型?

返回查看(员工);

是这样做的方法......
Read this article: What is ViewData, ViewBag and TempData? – MVC options for passing data between current and subsequent request[^]

What you should consider, is that TempData has a very short lifecycle, it is not passed between requests. It is meant for something else...
On the other hand why do you pass the model in TempData?
return View(employee);
is the way to do it...


这篇关于如何在视图中检索tempdata值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆