0

I have a button on a site that should - if clicked - pass some data to the controller and redirect to another view. I tried it with AJAX and realized it doesn't work, because it cannot redirect to another view.

I read some posts that suggested using JavaScript, but there wasn't one where a model is passed to the View as well (Propably I'm just dumb...).

Right now it looks like this:

function buttonClick() {
    $.ajax({
        type: 'POST',
        url: '/Backup/Timestamp',
        data: {iniName: selectedFile}
    });
}

public ActionResult Timestamp(string iniName)
{
    //some code

    return View(Model);
}

Thanks for your help!

6
  • Do you want this method 'Timestamp' as post only?? Commented Aug 1, 2017 at 10:34
  • You could pass the data to the controller, do some logic, and redirect to some another action which will open another view. Commented Aug 1, 2017 at 10:36
  • @Stefcho OP is using ajax. Ajax will never give you redirect result. Commented Aug 1, 2017 at 10:37
  • @Stefcho But how can I pass data when redirecting to another action? It doesn't seem like there is an overload that fits my needs. Commented Aug 1, 2017 at 10:54
  • @PowerStar If there is another method than Ajax I'd like to try it . What exactly do you mean with "'Timestamp' as post only"? Commented Aug 1, 2017 at 10:56

3 Answers 3

2

Instead of ajax, use the @Url.Action

// creates a url like this <domain>/Backup/Timestamp
// This should be in the cshtml
<script>
   var baseurl = '@Url.Action("Timestamp", "Backup")'; 
</script>

// in your custom js file which should added below the baseurl initialization
function buttonClick() {
    location.href = baseurl +'?iniName=' + selectedFile 
}

The @Url.Action() method is proccessed on the server-side. It will get the path for your action method. Then concatinate the parameter to that url and simply redirect to it

Sign up to request clarification or add additional context in comments.

Comments

1

On button click you can just redirect the page to some get method. So your parameter 'iniName' will be passed as query string instead of request body.

Because if you use GET method all the parameter will be passed as query string. If it is POST method then it will be send as request body.

So I think in your case Get is enough. So what you need to do is, you need to replace the "buttonClick" function like below.

function buttonClick() {
    location.href = '@Url.Action("Timestamp", "Backup")'+'?iniName=' + selectedFile 
}

5 Comments

I don't now whether I do something wrong, but now I get this URL: "localhost:32986/…"
@ErikT. is your buttonClick function in a seperate js file or in the cshtml itself?
@adiga it is in a separate js file
then you should do as the answer i have posted. Create the var baseurl = '@Url.Action("Timestamp", "Backup")'; in the cshtml inside a script tag. Then, add the script tag of your js file which has the buttonClick below it
@adiga Thx mate. That is what I was looking for!
0

You can use Ajax Action link as:

@Html.ActionLink("Edit", // <-- Link text
                 "Edit", // <-- Action Method Name
                 new { id=item.CustomerID }, // <-- Route arguments
                 new {@class="ui-btn", data_val="abc"} // <-- htmlArguments
                )

A complete detail for using Ajax Action link can be found at link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.