Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc MVC按钮未使用HttpPost触发ActionResult_Asp.net Mvc - Fatal编程技术网

Asp.net mvc MVC按钮未使用HttpPost触发ActionResult

Asp.net mvc MVC按钮未使用HttpPost触发ActionResult,asp.net-mvc,Asp.net Mvc,我对MVC和DevExpress比较陌生。我有一个UI,需要在其中获取日期字段并将其传递给控制器。非常简单的任务 我的看法是 <script type="text/javascript"> var positionDate = "1/1/2014"; function AddClick(s, e) { debugger; positionDate = LockedDateToAdd.GetDate().toDateString();

我对MVC和DevExpress比较陌生。我有一个UI,需要在其中获取日期字段并将其传递给控制器。非常简单的任务

我的看法是

<script type="text/javascript">
    var positionDate = "1/1/2014";

    function AddClick(s, e) {
        debugger;
        positionDate = LockedDateToAdd.GetDate().toDateString();

        $.ajax({
            type: "POST",
            url: "/LockedDate/AddLockedDate",
            data: { positionDate: positionDate },
            success: function (msg) { LockedDateGridView.PerformCallback(); }
        });
    }
</script>

@{
    ViewBag.Title = "Locked Dates";
}

<h4>Locked Dates</h4>

@using (Html.BeginForm())
{
    <table>
        <tr>
            <td>
                @Html.DevExpress().DateEdit(settings =>
                {
                    settings.Name = "LockedDateToAdd";
                    settings.Date = DateTime.Today;
                    settings.Properties.AllowNull = false;
                    settings.Properties.DateOnError = DateOnError.Today;
                    settings.Properties.CalendarProperties.ShowWeekNumbers = false;
                }).GetHtml()
            </td>
            <td>
                @Html.DevExpress().Button(settings =>
                {
                    settings.Name = "LockedDateAdd";
                    settings.Text = "Add";
                    settings.ClientSideEvents.Click = "AddClick";
                    settings.UseSubmitBehavior = false;
                }).GetHtml()
            </td>
        </tr>
    </table>
}
问题是,我的代码没有命中AddLockedDate ActionResult。当我删除[HttpPost]时,它确实会命中,但positionDate为空

我错过了什么?任何想法。
谢谢你的帮助

您的代码中似乎没有问题。只需按照以下步骤更新ajax方法

<script type="text/javascript">
    var positionDate = "1/1/2014";

    function AddClick(s, e) {
        debugger;
        positionDate = LockedDateToAdd.GetDate().toDateString();

        $.ajax({
            type: "POST",
            url: "/LockedDate/AddLockedDate",
            data: JSON.stringify({ positionDate: positionDate }),
            contentType: 'application/json; charset=utf-8',
            success: function (msg) { LockedDateGridView.PerformCallback(); }
        });
    }
</script>

var positionDate=“1/1/2014”;
功能添加单击(s、e){
调试器;
positionDate=LockedDateToAdd.GetDate().toDateString();
$.ajax({
类型:“POST”,
url:“/LockedDate/AddLockedDate”,
数据:JSON.stringify({positionDate:positionDate}),
contentType:'application/json;charset=utf-8',
成功:函数(msg){LockedDateGridView.PerformCallback();}
});
}

您的代码中似乎没有问题。只需按照以下步骤更新ajax方法

<script type="text/javascript">
    var positionDate = "1/1/2014";

    function AddClick(s, e) {
        debugger;
        positionDate = LockedDateToAdd.GetDate().toDateString();

        $.ajax({
            type: "POST",
            url: "/LockedDate/AddLockedDate",
            data: JSON.stringify({ positionDate: positionDate }),
            contentType: 'application/json; charset=utf-8',
            success: function (msg) { LockedDateGridView.PerformCallback(); }
        });
    }
</script>

var positionDate=“1/1/2014”;
功能添加单击(s、e){
调试器;
positionDate=LockedDateToAdd.GetDate().toDateString();
$.ajax({
类型:“POST”,
url:“/LockedDate/AddLockedDate”,
数据:JSON.stringify({positionDate:positionDate}),
contentType:'application/json;charset=utf-8',
成功:函数(msg){LockedDateGridView.PerformCallback();}
});
}

当客户端日期与JS的不同表示形式不能通过
DateTime.TryParse直接解析时,就会出现问题

使用
console.log(positionDate)
,捕获的日期格式如下(在当前编写时):

使用
TryParse
而不指定正确的日期格式,则无法解析日期格式,因此使用
TryParseExact
使用正确的格式:

控制器

[HttpPost]
public ActionResult AddLockedDate(string positionDate)
{
     DateTime dateValue;

     // parse exactly as toDateString method returns
     if (DateTime.TryParseExact(positionDate, "ddd MMM d yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue))
     {
         _dataAccess.AddLockedDate(dateValue);
     }

     return Content("");
}
查看

<script type="text/javascript">
    var positionDate = "1/1/2014";

    function AddClick(s, e) {
        debugger;
        positionDate = LockedDateToAdd.GetDate().toDateString();

        $.ajax({
            type: "POST",
            url: "@Url.Action("AddLockedDate", "LockedDate")",
            data: JSON.stringify({ positionDate: positionDate }),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (msg) { LockedDateGridView.PerformCallback(); }
        });
    }
</script>

var positionDate=“1/1/2014”;
功能添加单击(s、e){
调试器;
positionDate=LockedDateToAdd.GetDate().toDateString();
$.ajax({
类型:“POST”,
url:“@url.Action(“AddLockedDate”,“LockedDate”)”,
数据:JSON.stringify({positionDate:positionDate}),
数据类型:“json”,
contentType:“应用程序/json;字符集=utf-8”,
成功:函数(msg){LockedDateGridView.PerformCallback();}
});
}
相关的:

(MSDN)


(MSDN)

当来自JS的客户端日期的不同表示形式不能通过
DateTime.TryParse直接解析时,就会出现问题

使用
console.log(positionDate)
,捕获的日期格式如下(在当前编写时):

使用
TryParse
而不指定正确的日期格式,则无法解析日期格式,因此使用
TryParseExact
使用正确的格式:

控制器

[HttpPost]
public ActionResult AddLockedDate(string positionDate)
{
     DateTime dateValue;

     // parse exactly as toDateString method returns
     if (DateTime.TryParseExact(positionDate, "ddd MMM d yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue))
     {
         _dataAccess.AddLockedDate(dateValue);
     }

     return Content("");
}
查看

<script type="text/javascript">
    var positionDate = "1/1/2014";

    function AddClick(s, e) {
        debugger;
        positionDate = LockedDateToAdd.GetDate().toDateString();

        $.ajax({
            type: "POST",
            url: "@Url.Action("AddLockedDate", "LockedDate")",
            data: JSON.stringify({ positionDate: positionDate }),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (msg) { LockedDateGridView.PerformCallback(); }
        });
    }
</script>

var positionDate=“1/1/2014”;
功能添加单击(s、e){
调试器;
positionDate=LockedDateToAdd.GetDate().toDateString();
$.ajax({
类型:“POST”,
url:“@url.Action(“AddLockedDate”,“LockedDate”)”,
数据:JSON.stringify({positionDate:positionDate}),
数据类型:“json”,
contentType:“应用程序/json;字符集=utf-8”,
成功:函数(msg){LockedDateGridView.PerformCallback();}
});
}
相关的:

(MSDN)


(MSDN)

您使用的是什么版本的MVC?我看不出你的代码有什么问题。你读过这个吗?您使用的是什么版本的MVC?我看不出你的代码有什么问题。你读过这个吗?山本哲也,谢谢你的帮助。你的建议奏效了。山本哲也,谢谢你的帮助。你的建议奏效了。谢谢你,乔尼。你的建议很有帮助。我的问题现在解决了。谢谢你,乔尼。你的建议很有帮助。我的问题现在解决了。