cptc
cptc

Reputation: 87

Asp.net MVC Actionlink Adding Route Value Dynamically

I need sent system local datetime value to controller. I must use javascript for this but actionlinks listing Dynamically from database.

I try this but couldnt sent value to controller.

 function getLocalTime (obj) {
           debugger
            var time = new Date();
            var href = obj.getAttribute("href")
            href = href + '/&&LocalTime=' + time.toLocaleTimeString();
            this.href = href;

        }

And It's my actionlink

@Html.ActionLink(item.Name, "Index", "Restaurants", New With { .aaaa= Model.aaaa,.bbbb= item.bbbb},New With{.onmouseover="getLocalTime(this);"})

How to sent local time value to controller ?

EDIT :

controller here;

Function Index(Optional ByVal aaaa As aaaa= Nothing, Optional ByVal bbbb As Integer = 0, Optional ByVal cccc As Integer = 0, Optional ByVal LocalTime As DateTime = Nothing) As ActionResult

Route Here ;

  routes.MapRoute( _
name:="GetCitiesRestaurants", _
url:="Cities/SearchByCity/{aaaa}/{dddd}", _
defaults:=New With {.controller = "Cities", .action = "GetCitiesRestaurants", .aaaa= UrlParameter.Optional, .dddd= UrlParameter.Optional} _
)

I should hidden the varriables for privacy

Upvotes: 0

Views: 1621

Answers (1)

ramiramilu
ramiramilu

Reputation: 17182

How to sent localtime value to controller ?

Say you have an Anchor tag like this -

<a href="#" id="ClickMe">Click me</a>

Then you can use JQUERY AJAX to post the local date to server Controller in the following way. Have the following jquery click function associated to anchor tag -

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    $(document).ready(function () {
        $("#ClickMe").click(function () {
            var datetime = new Date();
            jQuery.ajax({
                type: "POST",
                url: "@Url.Action("SendDate")",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ date: datetime, cccc : 10, bbbb : 20 }),
                success: function (data) { alert(data); },
                failure: function (errMsg) { alert(errMsg); }
            });
        });
    });
</script>

Then if you have following controller action -

    public ActionResult SendDate(DateTime date, int cccc, int bbbb)
    {
        return null;
    }

And when you run the code with a breakpoint, you get date as shown below -

enter image description here

Upvotes: 2

Related Questions