简体   繁体   English

如何从我的视图传递参数到控制器动作?

[英]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} . 我会将动作网址作为常规MVC路由{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 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 MyController.cs

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

            return View();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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