简体   繁体   English

Ajax不会调用控制器方法MVC

[英]Ajax won't call controller method mvc

I am trying to do inline editing by cell, not by row, upon a double click. 我试图在双击时按单元而不是按行进行内联编辑。 This much works but it won't update the record- the "SaveCustomer" call to controller isn't made. 这很有效,但不会更新记录-不会对控制器进行“ SaveCustomer”调用。 What is it that I'm doing wrong? 我做错了什么事?

here is my index view file (part of it, the rest is irrelevant) 这是我的索引视图文件(一部分,其余部分不相关)

   @foreach (var item in Model.Drivers)
        {
            <tr id="@item.ID">
                <td id="id">
                     @Html.DisplayFor(modelItem => item.ID)
                </td>
                <td id="lastName">
                    @Html.DisplayFor(modelItem => item.LastName)
                </td>
                <td id="firstName">
                    @Html.DisplayFor(modelItem => item.FirstName) 
                </td>
                <td id="seniority">
                  @if (item.Seniority != null) {@Convert.ToDateTime(item.Seniority).ToShortDateString()}                                      
                </td>
                <td>
                <td> //more go here </td>

Then here is the javascript file- sorry for the formatting, happens everytime I copy&paste 然后这是javascript文件-抱歉,每次我复制粘贴时都会发生格式化

 $("td").dblclick(function () {
if (!$(this).hasClass("edit")) {
    var value = jQuery.trim($(this).html());
    $(this).html("<input id=\"txtEdit\" type=\"text\" class=\"textbox\" value=\"" + value + "\" onblur=\"SaveValue(this, '" + value + "')\" onfocus=\"PutCursorAtEnd(this)\" />");
    $(this).addClass("edit");
    $("#txtEdit").focus();
}
});

 function PutCursorAtEnd(obj) {
if (obj.value == obj.defaultValue) {
    $(obj).putCursorAtEnd(obj.length);
}
}

function SaveValue(obj, oldValue) {
var value = $(obj).val();
$(obj).parent().removeClass("edit");
if (value != oldValue) {
    var property = $(obj).parent().attr("id");
    var id = $(obj).parent().parent().attr("id");

    $.ajax({
        type: "POST",
        url: '@Url.Action("SaveCustomer", "save")',
        data: { ID: id, Property: property, Value: value },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: SaveCustomer_Success,
        error: Error
    });
 }
else {
    $(obj).parent().html(oldValue);
}
}

function SaveCustomer_Success(data, status) {
$(data.d.ID).parent().html(data.d.NewValue);
}

and lastly here is the controller method 最后是控制器方法

    public object SaveCustomer(int ID, string Property, string Value)
    {
        Driver driver = unitOfWork.DriverRepository.GetByID(ID);
        switch(Property) {
            case "id":
                driver.ID = Convert.ToInt32(Value);
                break;
            case "firstName":
                driver.FirstName = Value;
                break;
            case "lastName":
                driver.LastName = Value;
                break;
            case "seniority":
                driver.Seniority = Convert.ToDateTime(Value);
                break;
        }
        unitOfWork.DriverRepository.Update(driver);
        unitOfWork.Save();

        return new
        {
            ID = ID,
            NewValue = Value
        };
    }

Much help appreciated, thanks! 非常感谢您的帮助,谢谢!

Remove following line from your ajax options because you are not sending json type data. 从ajax选项中删除以下行,因为您没有发送json类型数据。

contentType: "application/json; charset=utf-8"

Since you are sending a POST request you have to add HttpPost attribute to controller. 由于要发送POST请求,因此必须将HttpPost属性添加到控制器。 And also you are expecting json result, so your controller should be like below. 而且您还期望json结果,所以您的控制器应如下所示。

[HttpPost]
public JsonResult SaveCustomer(int ID, string Property, string Value)
{
    /// ...
    return Json(new { ID = 1, NewValue = 1 }, JsonRequestBehavior.AllowGet);
}

Try removing data, contentType and dataType from Ajax call and change the url to: 尝试从Ajax调用中删除数据,contentType和dataType并将URL更改为:

url: 'save/SaveCustomer?ID=' + id + '&Property=' + property + '&Value=' + value,

put a break point in the controller to see if the call hits it. 在控制器中放置一个断点,以查看呼叫是否成功。 There is no reason why it will not. 没有理由不这样做。

Use Chrome browser and open JavaScript console. 使用Chrome浏览器并打开JavaScript控制台。 You will see why it doesn't work, if it doesn't work after this change. 如果更改后它不起作用,您将看到为什么它不起作用的原因。

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

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