Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:22:50+00:00 2026-05-23T19:22:50+00:00

I am redirecting the view from [HttpPost] method to [HttpGet] method. I have gotten

  • 0

I am redirecting the view from [HttpPost] method to [HttpGet] method. I have gotten it to work, but want to know if this is the best way to do this.

Here is my code:

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

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:22:51+00:00Added an answer on May 23, 2026 at 7:22 pm

    There are basically 3 techniques in ASP.NET MVC to implement the PRG pattern.

    • TempData

    Using TempData is indeed one way of passing information for a single redirect. The drawback I see with this approach is that if the user hits F5 on the final redirected page he will no longer be able to fetch the data as it will be removed from TempData for subsequent requests:

    [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); 
    }
    
    • Query string parameters

    Another approach if you don’t have many data to send is to send them as query string parameters, like this:

    [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); 
    }
    
    • Persistence

    Yet another approach and IMHO the best consists into persisting this model into some data store (like a database or something and then when you want to redirect to the GET action send only an id allowing for it to fetch the model from wherever you persisted it). Here’s the pattern:

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

    Each method has its pros and cons. Up to you to choose which one suits best to your scenario.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my project i have a side menu from which I am redirecting to
So I'm clicking on this link: <a href=intent://www.google.com#Intent;scheme=http;action=android.intent.action.VIEW;end>Google</a> (I know its a silly example,
I have Three type of gestures in my view(tableview is my view) PinchGesture:redirecting to
my question seems simple and stupid but first read this, suppose you have a
Is there any other command for redirecting a controller to a particular view page
I'm having problems redirecting stdio of another program using subprocess module. Just reading from
How to redirecting from win forms to web forms ?
I'm having trouble with redirecting urls using the .htaccess file. This is what my
Is there a simple way of redirecting serial port output to a file, that
I have a situation that requires redirecting users who are already logged in away

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.