asp.net-mvc – 将变量从[HttpPost]方法传递给[HttpGet]方法
发布时间:2020-12-16 07:27:41 所属栏目:asp.Net 来源:网络整理
导读:我正在将视图从[HttpPost]方法重定向到[HttpGet]方法.我已经开始工作,但想知道这是否是最好的方法. 这是我的代码: [HttpPost] public ActionResult SubmitStudent(){ StudentViewModel model = TempData["model"] as StudentResponseViewModel; TempData["i
|
我正在将视图从[HttpPost]方法重定向到[HttpGet]方法.我已经开始工作,但想知道这是否是最好的方法.
这是我的代码: [HttpPost]
public ActionResult SubmitStudent()
{
StudentViewModel model = TempData["model"] as StudentResponseViewModel;
TempData["id"] = model.Id;
TempData["name"] = model.Name;
return RedirectToAction("DisplayStudent");
}
[HttpGet]
public ActionResult DisplayStudent()
{
ViewData["id"] = TempData["id"];
ViewData["name"] = TempData["name"];
return View();
}
视图: <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> <html> <head runat="server"> <title>DisplayStudent</title> </head> <body> <div> <%= ViewData["id"]%> <br /> <%= ViewData["name"]%> </div> </body> </html> 解决方法
ASP.NET MVC中基本上有3种技术来实现
PRG pattern.
> TempData 使用TempData确实是传递单个重定向信息的一种方法.我看到这种方法的缺点是,如果用户在最终重定向页面上点击F5,他将无法再获取数据,因为它将从TempData中删除以用于后续请求: [HttpPost]
public ActionResult SubmitStudent(StudentResponseViewModel model)
{
if (!ModelState.IsValid)
{
// The user did some mistakes when filling the form => redisplay it
return View(model);
}
// TODO: the model is valid => do some processing on it
TempData["model"] = model;
return RedirectToAction("DisplayStudent");
}
[HttpGet]
public ActionResult DisplayStudent()
{
var model = TempData["model"] as StudentResponseViewModel;
return View(model);
}
>查询字符串参数 如果您没有很多要发送的数据,另一种方法是将它们作为查询字符串参数发送,如下所示: [HttpPost]
public ActionResult SubmitStudent(StudentResponseViewModel model)
{
if (!ModelState.IsValid)
{
// The user did some mistakes when filling the form => redisplay it
return View(model);
}
// TODO: the model is valid => do some processing on it
// redirect by passing the properties of the model as query string parameters
return RedirectToAction("DisplayStudent",new
{
Id = model.Id,Name = model.Name
});
}
[HttpGet]
public ActionResult DisplayStudent(StudentResponseViewModel model)
{
return View(model);
}
>坚持 另一种方法和恕我直言最好的方法是将这个模型持久化到一些数据存储(如数据库或其他东西,然后当你想重定向到GET操作时只发送一个id,允许它从你持久化的地方获取模型) .这是模式: [HttpPost]
public ActionResult SubmitStudent(StudentResponseViewModel model)
{
if (!ModelState.IsValid)
{
// The user did some mistakes when filling the form => redisplay it
return View(model);
}
// TODO: the model is valid => do some processing on it
// persist the model
int id = PersistTheModel(model);
// redirect by passing the properties of the model as query string parameters
return RedirectToAction("DisplayStudent",new { Id = id });
}
[HttpGet]
public ActionResult DisplayStudent(int id)
{
StudentResponseViewModel model = FetchTheModelFromSomewhere(id);
return View(model);
}
每种方法都有其优点和缺点.由您决定哪一个最适合您的场景. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 为什么ASP.Net MVC便携式区域不像Rails插件
- ASP.NET linkbutton两次提高onBeforeUnload事件
- asp.net – HttpContext.Request.Cookies和HttpContext.Res
- ASP.NET数据从代码隐藏双向双向绑定
- asp.net-mvc – Olark免费替代品
- asp.net – 在代码中设置PageSize时,DataPager停止工作
- asp.net-mvc – [Authorize(Users =“*”)]在asp.net mvc中
- 必须是ASP.NET服务器控件属性属性
- asp.net-mvc – ASP.NET MVC主页数据
- asp.net-mvc – 传递IEnumerable变量以在ASP.NET MVC中查看
推荐文章
站长推荐
- ASP.NET MVC:如何使用控制器发送HTML电子邮件?
- asp.net-mvc-3 – 使用jQuery验证货币字段的客户
- asp.net-mvc-5 – MVC 5 AttributeRouting Catch
- asp.net-mvc – 当前动作是ChildAction吗?
- asp.net-mvc-4 – ASP.NET Web API授权令牌提前到
- asp.net-mvc – 使用MVC3剃刀的ASP.Net图表控件
- ASP.NET MVC和ORM选择
- 更改ASP.NET中错误消息的语言
- asp.net-mvc – 将依赖项注入验证属性Web Api As
- asp.net – Web应用程序 – 结合还是分开?
热点阅读
