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 7504107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:27:43+00:00 2026-05-29T21:27:43+00:00

I have the following actions in my home controller. when i post to the

  • 0

I have the following actions in my home controller. when i post to the GetDistance() it never goes to the /Home/ShowDistance page but stays on the /Home/Index page. I have checked that the lat and lon values are not null so the browser should be redirected to /Home/ShowDistance.

public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult GetDistance(string lat, string lon)
{
    if (lat != null && lon != null)
    {
        Session["latlon"] = new LatLon { Latitude = double.Parse(lat), Longitude = double.Parse(lon) };
        return RedirectToAction("ShowDistance");
    }
    return RedirectToAction("Index");    
}

public ActionResult ShowDistance()
{
    //...
    return View();
}
  • 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-29T21:27:44+00:00Added an answer on May 29, 2026 at 9:27 pm

    In your Index.cshtml view make sure that you specify the correct controller action to post to (because it has a different name than the one used to render the form). So instead of:

    @using (Html.BeginForm())
    {
        ...
    } 
    

    use:

    @using (Html.BeginForm("GetDistance", null))
    {
        ...
    } 
    

    Or if you don’t use a standard HTML <form> to invoke the GetDistance action but an AJAX call, then it’s perfectly normal that the browser url stays at /Home/Index. The whole point of AJAX is to perform an asynchronous HTTP request to the server without navigating away from the current page (which in your case is /Home/Index). If this is the case and you want to redirect you will have to do it on the client in the success callback:

    $.ajax({
        url: '@Url.Action("GetDistance")',
        type: 'POST', 
        data: { lat: '123', lon: '456' },
        success: function(result) {
            window.location.href = result.redirectTo;
        }
    });
    

    You will also have to modify your GetDistance POST action so that instead of redirecting it returns a JSON object containing the target url to redirect to that can be used in the success callback:

    [HttpPost]
    public ActionResult GetDistance(string lat, string lon)
    {
        if (lat != null && lon != null)
        {
            Session["latlon"] = new LatLon { Latitude = double.Parse(lat), Longitude = double.Parse(lon) };
            return Json(new { redirectTo = Url.Action("ShowDistance") });
        }
        return Json(new { redirectTo = Url.Action("Index") });
    }
    

    Obviously this kind of defeats the purpose of AJAX a little because as I said the whole point of AJAX is to perform asynchronous requests to the server and stay on the same page. So in this case you should probably stick with a standard HTML <form>.

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

Sidebar

Related Questions

I have a standard User controller with the normal set of actions (index, show,
i have home controller. my home controller has two index method one with no
I'm using MVC3 and in my view I have the following code block; Response.Redirect(@Url.Action(index,home,
I have two database tables with the following structure: actions: action_id int(11) primary key
I have the following controller action: [HttpGet] public JsonpResult getTestValues(int? entityId, int? id) {
I have the following index action: class ExpensesController < ApplicationController def index() @expenses =
I have the following models but I cannot get the custom method reorder_action_items to
I have a route that looks like the following: map.newsletter '/newsletter', :controller => newsletter
I have a user control which contains the following code: <form id=CurrencyForm method=post runat=server>
I am having problem with namespaced controller and routing. I have following namespaced route.

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.