weixin_33727510 2016-02-18 10:42 采纳率: 0%
浏览 22

将日期时间发布到控制器

I am trying to send date to controller using ajax but get's null. why?

enter image description here

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<script src="~/Scripts/jquery-2.2.0.min.js"></script>
<script src="~/Scripts/moment.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/bootstrap-datetimepicker.min.js"></script>

<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<link href="~/Content/bootstrap-datetimepicker.min.css" rel="stylesheet" />

<div class="container">
<div class="row">
    <div class='col-sm-6'>
        <div class="form-group">
            <div class='input-group date' id='datetimepicker1'>
                <input type='text' class="form-control" />
                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        $('#datetimepicker1').datetimepicker({ useCurrent: false });
        $('#datetimepicker1').on("dp.hide", function (e) {
            $.ajax({
                url: "/Home/GetData",
                type: "POST",
                data: JSON.stringify($('#datetimepicker1').data('DateTimePicker').date()),
                contentType: "application/json",
                success: function (result) { alert('Done') },
                error: function (r, e, s) { alert(e) }
            });
        });
    </script>
</div>
</div>

Controller:

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public ActionResult GetData(string test)
    {
        return View();
    }
}
  • 写回答

1条回答 默认 最新

  • weixin_33738555 2016-02-18 12:04
    关注

    You not passing a name/value pair back to the controller method that matches the parameter name (test) and the is no need to stringify the data. Change the ajax script to

    $.ajax({
        url: '@Url.Action("GetData", "Home")', // don't hard code your url's 
        type: "POST",
        data: { test: $('#datetimepicker1').data('DateTimePicker').date() },
        // contentType: "application/json", delete this
        success: function (result) { alert('Done') },
        error: function (r, e, s) { alert(e) }
    });
    

    And since you posting a DateTime value the controller method should be

    [HttpPost]
    public ActionResult GetData(DateTime test)
    {
        return View();
    }
    

    This assumes the the date value is in a format that matches the server culture, or in ISO format ('yyyy-MM-dd HH:mm'), for example by using

    data: { test: $('#datetimepicker1').data('DateTimePicker').date().format('YYYY-MM-DD HH:mm') },
    

    Note that your method is returning a view, but you not doing anything with the html you return (just displaying an alert)

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建