简体   繁体   中英

How to pass a parameter to controller action from my view?

I'm trying to call an action from my view passing a paremeter value that's my code

  window.location.href = '@Url.Action("Index", "RelatoriosConsolidados")';

to pass some parameter I can do this

  window.location.href = '@Url.Action("Index", "RelatoriosConsolidados", 
                          new { param1 = "Value"})';

but I cant get a value from a datepicker

@(Html.Kendo().DatePicker()
          .Name("#datepicker")
          .Events(e => e.Change("change"))
          .Min(new DateTime(2012, 7, 1))

          .Value(Model.dataInicial)
          .HtmlAttributes(new { style = "width:150px" })

   function change() 
   {
    var x = kendo.toString(this.value(), 'd');
    window.location.href = '@Url.Action("Index", "RelatoriosConsolidados",
     new { data = x })';
   }

How can I do that?

I would pass the url of action as conventional MVC route {controller}/{Action}/{Id} . And then get the passed parameter (with same parameter name) in your action. My implementation would be something like this:

View.cshtml

$(document).ready(function () {
            // create DatePicker from input HTML element
            $("#datepicker").kendoDatePicker();

            $('#btnSubmit').click(function() {
                alert($('#datepicker').val());
                var data = $('#datepicker').val();
                window.location.href = "/Home/Contact/" + data;
            });
       });

MyController.cs

public ActionResult Contact(string data)
        {
           //do something with 'data'
            ViewBag.Message = "Your contact page.";

            return View();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM