asp.net-mvcredirecttoaction

View doesn't refresh after RedirectToAction is done


Here is my problem:

[HttpPost]
public ActionResult AddData(CandidateViewModel viewModel)  
{
    var newCandidateId = 0;  
    newCandidateId = this._serviceClient.AddCandidate(viewModel);  
    return  RedirectToAction("DisplayCandidate",new {id=newCandidateId});  
}

public ActionResult DisplayCandidate(int id)
{
    var candidateViewModel= this._serviceClient.GetCandidate(id);
    return View(candidateViewModel);
}

After filling the form viwemodel sends to server. After data were stored, flow is redirected to DisplayCandidate action and it goes there but page didn't refresh. I don't understand why! Help, please.


Solution

  • Because you are using Ajax Post

    public ActionResult AddData(CandidateViewModel viewModel)  
    {
        var newCandidateId = 0;  
        newCandidateId = this._serviceClient.AddCandidate(viewModel); 
        string ReturnURL = "/DisplayCandidate/"+newCandidateId;
        return JSON(ReturnURL);  
    }
    

    and in your Ajax Post Method:

    Onsuccess(function(retURL){ window.location(retURL); })
    

    This will take to the new Action and that Action will return View.