繁体   English   中英

从视图获取参数到控制器

[英]Get parameter from view to controller

我正在使搜索框根据发送的值来获取数据库的一个寄存器,因此在我看来:

<p> Manifest: <input type="text" name="manifest" id="manifest" /> <input type="submit" value="search" name="btnGetForEdith" id="btnGetForEdith" /></p>

JS:

$(document).ready(function () {
    GetModulLogWasteForEdit();
    $("#btnGetForEdith").click(onGetModulLogWasteSuccess);
});

function GetModulLogWasteForEdit() {
    currentId = 0;
    try {
        $(function () {
            $.ajax({
                cache: false,
                type: "get",
                dataType: "json",
                url: "/LogWaste/GetForEdit",
                contentType: "application/json; charset=utf-8",
                success: onGetModulLogWasteSuccess,
                error: function (response) {
                    ErrorMessage("Error", GetTextError(response));
                }
            });
        });
    } catch (e) {
        ErrorMessage("Error", e.message);
    }
}

控制器:

public ActionResult GetForEdit(string manifest)
        {
            string result = string.Empty;
            var userId = User.Identity.GetUserId();
            var currentUser = UserClass.GetUserBranchOfficeId(userId);
            try
            {
                result = new JavaScriptSerializer().Serialize(LogWasteModule.GetForEdit(currentUser));
            }
            catch (Exception)
            {
                throw;
            }

            return Content(result, "application/json");
        }

问题是我没有在控制器中获取“清单”值,所以它为null所以我无法使用它。 谁能解释我为什么会这样? 问候

尝试在放置了ajax请求的地方执行匿名函数

function GetModulLogWasteForEdit() {
currentId = 0;
try {
    $(function () {
        $.ajax({
            cache: false,
            type: "get",
            dataType: "json",
            url: "/LogWaste/GetForEdit",
            contentType: "application/json; charset=utf-8",
            success: onGetModulLogWasteSuccess,
            error: function (response) {
                ErrorMessage("Error", GetTextError(response));
            }
        });
    })(); // Add () to execute the function
} catch (e) {
    ErrorMessage("Error", e.message);
}

}

 function GetModulLogWasteForEdit() {
  currentId = 0;


try {
     var manifest=$('#manifest').val();

        $.ajax({
            cache: false,
            type: "get",
            dataType: "json",
            url: "/LogWaste/GetForEdit",
            data:{manifest:manifest}
            contentType: "application/json; charset=utf-8",
            success: onGetModulLogWasteSuccess,
            error: function (response) {
                ErrorMessage("Error", GetTextError(response));
            }
        });
    } catch (e) {
     ErrorMessage("Error", e.message);
    }
}

为了获得价值,您必须发送它,我认为您没有这样做。 添加:

data: {'manifest': manifest }

所以它应该看起来像这样:

$(function () {
        $.ajax({
            cache: false,
            type: "get",
            dataType: "json",
            url: "/LogWaste/GetForEdit",
            data: {'manifest': manifest }
            contentType: "application/json; charset=utf-8",
            success: onGetModulLogWasteSuccess,
            error: function (response) {
                ErrorMessage("Error", GetTextError(response));
            }
        });
    });

我希望这有帮助!

请在Javascript中使用以下代码

 $(document).ready(function () {           
        $("#btnGetForEdith").click(function () {
            GetModulLogWasteForEdit();
        });
    });

    function GetModulLogWasteForEdit() {
        currentId = 0;
        try {
            $(function () {
                $.ajax({
                    cache: false,
                    type: "GET",
                    dataType: "json",
                    url: "/LogWaste/GetForEdit?manifest=" + $("#manifest").val(),//Manifest will be passed in querystring like this
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        console.log(data);
                    },
                    error: function (response) {
                        console.log("Error", GetTextError(response));
                    }
                });
            });
        } catch (e) {
            ErrorMessage("Error", e.message);
        }
    }

在控制器中

 public ActionResult GetForEdit(string manifest)
    {
        string result = string.Empty;
        var userId = User.Identity.GetUserId();
        var currentUser = UserClass.GetUserBranchOfficeId(userId);
        try
        {
            result = new JavaScriptSerializer().Serialize(LogWasteModule.GetForEdit(currentUser));
        }
        catch (Exception)
        {
            throw;
        }
        //Had changed this line as your are using `dataType: "json"`, as return data type
        return Json(result, JsonRequestBehavior.AllowGet);
    }

暂无
暂无

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

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