【问题标题】:How to Pass DateTimePicker Value to Controller from VIew?如何将 DateTimePicker 值从 VIew 传递给控制器​​?
【发布时间】:2015-09-04 07:00:14
【问题描述】:

我想将 datetimepicker 值传输到我的控制器。我可以通过常规方式转移价值:

     //PassDate Value While Calling Main Controller.
    vrdCurv = "@Url.Action("GetOutgoingMessage", "OutGoingMessages", new { ABC = "asdnfsdf" })";

但无法从下面的代码中得到结果。

  //******Start of Date Pickers************
    var datepickerFrom = $("#dateFromPicker").kendoDatePicker({
        format: "dd/MM/yyyy",
        change: function () {
            datepickerTo.min(datepickerFrom.value());
        },
    }).data("kendoDatePicker");

  //PassDate Value While Calling Main Controller.
    vrdCurv = "@Url.Action("GetOutgoingMessage", "OutGoingMessages", new { dateTimePicker.value })";

//控制器代码:

     [HttpGet]
    public JsonResult GetOutgoingMessage(string ABC)
    {
    var a = abc;

    }

我还尝试传递正常变量值,如下代码也不起作用:

   [HttpGet]
    public JsonResult GetOutgoingMessage(DateTime? abc)
    {
        using (var db = SiteUtil.NewDb)
        {

             TempData["msg"] = abc;
          }
    }

//Jscript:

    var fromDate = null;
    var toDate = null;

    //Inilialise date variable.

    fromDate = '2015-07-10';
    toDate = '2015-07-10';

    var dataSourceOutMessage = new kendo.data.DataSource({
            transport: {
            read: {
                url: "@Url.Action("GetOutgoingMessage", "OutGoingMessages")",
               dataType: 'json',
               date:{"fromdate" : fromdate}
        }
    },
        schema: {
      model: {
      fields:
     {
     Id: { type: "String" },
     MsgType: { type: "String" },
     Subject: { type: "String" },
     CreatedOn: { type: "String" },
    ProcessedOn: {type: "date"}
   }
   }
   },
   pageSize: 20
   });

【问题讨论】:

  • @Url.Action() 是剃须刀代码,它在发送到视图之前在服务器上进行评估。您不能使用new { dateTimePicker.value } - 因为服务器上不存在dateTimePicker。你需要使用ajax来发布值
  • 我尝试传递也不起作用的正常变量。
  • 你需要展示你尝试过的代码

标签: asp.net-mvc model-view-controller kendo-ui kendo-datepicker


【解决方案1】:

你可以试试这个:

 vrdCurv = "@Url.Action("GetOutgoingMessage", "OutGoingMessages", new { ABC = "-1"})";
 vrdCurv.replace("-1", datepickerFrom.value());

(很脏,但我认为这会起作用)

我强烈建议你从 ASP.NET MVC here 开始学习。

【讨论】:

  • 感谢您的链接。我一定会关注这个链接。
  • 谢谢。是的,它的工作原理。但我也得到了解决方案。问题是参数名称。无论是控制器还是视图,这应该是相同的。我做了同样的事情并得到了解决方案。现在醒了。
猜你喜欢
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多