0

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

1 Answer 1

2

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

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

5 Comments

thanks for reply but i sent another values with localtime value.
im edited question can you look added paragraf please ?
@cptc, I updated my answer with additional parameters. Please check it.
I understand now. I change your document.ready to normal function and i call this in a tag on click event. When clicked a sent my parameters to js function and sending all parameters to controller. Thank you so much !
how to use this without ajax ? Becouse now data values sending controller and controller returns view but page not redirecting. I guess for ajax ?

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.