【问题标题】:Make jquery ajax calls to ASP.NET MVC action对 ASP.NET MVC 操作进行 jquery ajax 调用
【发布时间】:2012-12-22 16:23:34
【问题描述】:

我在 ASP.NET MVC 中有一个简单的编辑操作,如下所示:

[HttpPost]
public ActionResult Edit(EditPostViewModel data)
{
}

我正在尝试发布这样的操作:

function SendPost(actionPath) {
    $.ajax({
        url: actionPath,
        type: 'POST',
        dataType: 'json',
        data: '{Text=' + $('#EditPostViewModel_Text').val() + 'Title=' + $('#EditPostViewModel_Title').val() + '}',
        success: function (data) {
            alert('success');
        },
        error: function () {
            alert('error');
        }
    });
}

动作将被触发,但 EditPostViewModel 不会填充 Text 和 Title?

我希望我可以使用常规的 ASP.NET MVC 操作来使用 ModelState 在服务器端处理验证。

稍后会有处理返回数据的成功和错误代码。

这应该如何工作?

【问题讨论】:

    标签: javascript jquery ajax asp.net-mvc action


    【解决方案1】:

    试试这个:

    data: 
    {
         Text: $('#EditPostViewModel_Text').val(),
         Title: $('#EditPostViewModel_Title').val() 
    }
    

    【讨论】:

    • 给OP:需要提供一个有效的JS对象给AJAX方法;您的语法(使用 = 而不是 :)不会产生有效的对象。
    猜你喜欢
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多