21

How to refresh the current page in MVC.

[HttpGet]
public ActionResult Request()
{
    if (Session["type"] != null  && Session["resulttype"] != null)
    {
        return View();
    }
    else
    {
        return null;
    }
}

I want to refresh my page in else part. That is when return null value.

5
  • 1
    you can use Redirect or RedirectToAction to reload Commented Mar 19, 2014 at 5:24
  • 1
    Its not working....because the browser show that its redirect to another page and again redirect to another...ie, a cycle formed.. Commented Mar 19, 2014 at 5:45
  • What do you want to achieve? This does not seem right Commented Mar 19, 2014 at 5:46
  • just want to refresh the current page...\ Commented Mar 19, 2014 at 5:50
  • if you refresh page again cycle is formed...it will again come in else part... better redirect to another page Commented Mar 19, 2014 at 5:52

4 Answers 4

38

You can use Request.UrlReferrer.ToString()

[HttpGet]
public ActionResult Request()
{

    if (Session["type"] != null  && Session["resulttype"] != null)
        return View();
    else
        return Redirect(Request.UrlReferrer.ToString());
}
Sign up to request clarification or add additional context in comments.

Comments

8

You can use the following code in asp.net core

public IActionResult Index(){
         return Redirect($"{Request.Path.ToString()}{Request.QueryString.Value.ToString()}");
}

Comments

6

Just Redirect to the Action you want to redirect to. It will refresh your page.

    [HttpGet]
    public ActionResult Request()
    {
        if (Session["type"] != null  && Session["resulttype"] != null)
        {
            return View();
        }
        else
        {
            return RedirectToAction("Request");
        }
    }

3 Comments

@Indianprogrammer According to Object Oriented Programming concept=> If you will return the 'RedirectToAction' then your method execution completes. else 'RedirectToAction' will be used as a Method for Processing something.
This won't refresh the page. Suppose you are on the Home Controller's Index Action and you make changes to the page by performing a post function. If you use RedirectToAction("Index"), this won't refresh the page as the control is still on the Index action of the Home controller.
@Aditya Have you tested that? RedirectToAction always returns a redirect to the browser, regardless whether it's the same page. This will prompt browser to redirect. If it's the same page the effect is a refresh.
4

You can use location.href = location.href; in Javascript code after calling a buttonclick or after a action method call like.

$('#btnme').click(function () {
   location.href = location.href;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.