BoundForGlory
BoundForGlory

Reputation: 4427

Refresh page with new query string ....MVC

I have an MVC 3 project where you can edit a record, or clone it. When you click on the record to edit, the url looks like this:

    http://localhost/Products/Edit/?Id=123

If the user clicks clone, a popup window appears asking for a new name. The user enters a name then clicks proceed. An ajax call is made with the old id and new name:

         var formData = "Id="[email protected]+"&Name="+$('#txt').val();
        $.ajax({
            url: '@Url.Action("Clone")',
            type: "POST",
            data: formData,
            success: function(resp) {
                    //this is where i try to refresh the page with the newly created id
                    window.location.href = "@(Url.Action("Edit") + "/?Id=" + resp.NewId)";

            },
            error:function(data) {
            },
            complete:function() {
            }
        });

Here's my action:

 public ActionResult Copy(Inputmodel viewModel)
    {
        //this will populate the new id inside of my model object
        var model = Clone(viewModel);
        return View("Edit", model);
   }

Now i need to reload the browser with the newly created id, so the url should look like this:

    http://localhost/Products/Edit/?Id=456

I tried the above in my ajax call, but that didn't work. How do i achieve what i'm trying to do? Thanks

Upvotes: 1

Views: 1496

Answers (2)

Anupam Singh
Anupam Singh

Reputation: 1166

You are looking for RedirectToAction.

Try replacing:

return View("Edit", model); 

with

return RedirectToAction("Edit","Products",new {Id="456"}/*set query string*/);

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

You are not concatenating right.

Do it like this:

window.location.href = "@Url.Action("Edit")"+"?Id=" + resp.NewId;

Or:

window.location.href = "@Url.Action("Edit")?Id=" + resp.NewId;

Upvotes: 2

Related Questions