通过AJAX将单个列表项从视图发送到控制器

时间:2015-10-02 13:36:30

标签: javascript jquery ajax asp.net-mvc

我有一个由用户列表组成的视图:

 @model List<LabelPrinting.Models.UserModel>

我把它们放到一个JavaScript对象中:

 users = function () { return @Html.Raw(Json.Encode(Model)) }();
然后我加载一个带有每个值的jQuery手风琴。然后,我选择在列表中为特定用户打印avery标签并设置值。我试图仅将该特定用户发送到控制器并获取用户属性的空值:

 function PrintAveryLabel(but) {
    var id = but.id.substring(9);
    var $rad = $(but).closest('tr').siblings().find('.radLabelOther');
    if (($rad).is(':checked')) {
        var $txtr = $rad.closest('tr').siblings().find('.classRows');
        var $txtc = $rad.closest('tr').siblings().find('.classCols');
        if ($txtr.val() === "0" || $txtc.val() === "0") {
            $("#lblError").text("You have have not selected the rows and columns for the labels.");
            $("#MessageDialog").dialog({ title: "Select Rows/Columns" });
            $("#MessageDialog").dialog("open");
            return false;
        }
    }
    var data = findUser(id);
    $.ajax({
        type: 'POST',
        data: { pat: data },
        url: '@Url.Action("PrintUserLabels")'
    });
}

findUser函数只是选择列表中与ID匹配的条目。

 function findUser(id) {
    var data;
    for (i = 0; i < cnt; i++) {
        if (users[i].UserId === parseInt(id)) {
            data = users[i];
            break;
        }
    }
    return data;
}

我的控制器操作:

 [HttpPost]
    public ActionResult PrintUserLabels(UserModel pat)
    {
        string retval = "";
        if (pat.PrintLabel)
        {
            return RedirectToAction("Label", new { user = pat });
        }
        else
        {
            retval = "ERROR - you must make a selection";
            Exception e = new Exception(retval);
            HandleErrorInfo info = new HandleErrorInfo(e, "DYMO", "PrintPatientLabels");
            return RedirectToAction("Error", info);
        }
    }

尝试使用标签操作,但我获得了一个空用户模型。我做错了什么?

0 个答案:

没有答案
相关问题