加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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); 
}

每种方法都有其优点和缺点.由您决定哪一个最适合您的场景.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读