Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new to Javascript. I need help in the following scenario:

I have 2 dropdowns in my view, and a Anchor(a) element. On click of this anchor element, I want to redirect to another view(basically have to call another action function), but I need to pass the current values which are being selected in those 2 drop downs. In short, I need to call another action function and pass the 2 current values selected in drop downs with it.

What I have tried:

I have written the following Ajax code, however it is always going to the error part:
JavaScript
$.ajax({
    type: 'GET',
    url: Url.Action("ActionMethod", "MyController"),
    data: {
        param1: DD1Value,
        param2: DD2Value,
        param3: xyz
    },
    success: function (data) {
        window.location.href = data.redirecturl;
    },
    error: function () {
        alert('error happened');
    }
}); 


Controller:
C#
[HttpGet]
public ActionResult ActionMethod(DateTime param1, DateTime param2, string param3)
{
//Do some work
//Return View
}


Kindly Help.
Posted
Updated 29-Mar-16 23:27pm
v3
Comments
Kornfeld Eliyahu Peter 30-Mar-16 3:35am    
And what the error is?
NJ44 30-Mar-16 4:16am    
It is going to the function error instead of Success.
Kornfeld Eliyahu Peter 30-Mar-16 4:17am    
But, what the error is?!
Karthik_Mahalingam 30-Mar-16 5:14am    
post your ActionMethod code..
some error in that action.

Try this..
Note the inline comments

JavaScript
function fungo() {
        var date = new Date(); // Javascript date object 
        var link = '@Url.Action("ActionMethod", "Home")';  // url should be enclosed by single quotes.
        var args = {
            param1: date.toISOString(),  // make sure that the date is in Javascript date object and converted to ISO string for proper casting in c#
            param2: date.toISOString(),
            param3: 'somevalue'
        }; 

        $.ajax({
            type: "GET",
            url: link, // url of your action
            data: args, // parameters if available 
            dataType: "json",
            success: function (data) {

                window.location.href = data.redirecturl; // your action should return an object having [redirecturl] property

            },
            error: function (httpRequest, textStatus, errorThrown) {  // detailed error messsage 
                alert("Error: " + textStatus + " " + errorThrown + " " + httpRequest);
            }
        });

    }


Controller:

C#
[HttpGet]
    public ActionResult ActionMethod(DateTime param1, DateTime param2, string param3)
    {
        return Json(new { redirecturl = "http://www.codeproject.com/" }, JsonRequestBehavior.AllowGet);

    }
 
Share this answer
 
Comments
NJ44 5-Apr-16 2:25am    
Thanks Karthik
Karthik_Mahalingam 5-Apr-16 2:31am    
welcome NJ44 :)
You can do this by creating the url and redirecting to it

HTML
<a href="#" id="mylink">Click</a>

<script type="text/javascript">
    $(document).ready(function () {
        $("#mylink").click(test);
    });

    function test() {
        var DD1Value = '2016-03-30 00:00:00';
        var DD2Value = '2016-04-01 00:00:00';
        var xyz = "Helllo world";

        var url = '@Url.Action("ActionMethod", "Home")';
        url += "?param1=" + escape(DD1Value);
        url += "&param2=" + escape(DD2Value);
        url += "&param3=" + escape(xyz);

        window.location.href = url;
    }
</script>


Or by wrapping your elements in a form and submitting it

HTML
@using (Html.BeginForm("ActionMethod", "Home", FormMethod.Get, new { @id = "myform" }))
{
    <input type="text" name="param1" value="2016-03-30 00:00:00" />
    <input type="text" name="param2" value="2016-04-01 00:00:00" />
    <input type="text" name="param3" value="Hello world" />
}

<a href="#" id="mylink">Click</a>

<script type="text/javascript">
    $(document).ready(function () {
        $("#mylink").click(function () {
            $("#myform").submit();
        });
    });
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900