0

I´m making searchbox to get one register of my database depending of value sended, so in my view:

<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);
    }
}

Controller:

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");
        }

Problem is I don´t getting "manifest" value into my controller it come null so I can´t play with it. Can anyone explain me why it happens? Regards

Jesus A.
  • 69
  • 1
  • 8
  • You are missing the data parameter on the ajax call... See this: http://stackoverflow.com/questions/15576548/how-to-pass-parameters-in-get-requests-with-jquery – Icaro Bombonato Nov 23 '16 at 18:10

4 Answers4

0

Try to execute the anonymous function where you did put the ajax request

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);
}

}

beta-developper
  • 1,689
  • 1
  • 13
  • 24
  • Thats not the problem, problem is I don´t received "manifest" value of input search I write before press "search" button (view) into my controller – Jesus A. Nov 23 '16 at 18:04
0
 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);
    }
}
0

In order to receive a value you have to send it, which I don't see you are doing. Add this:

data: {'manifest': manifest }

So it should look like this:

$(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));
            }
        });
    });

I hope this helps!

Grizzly
  • 5,873
  • 8
  • 56
  • 109
0

Please use following code in 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);
        }
    }

In controller

 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);
    }
J-Mean
  • 1,192
  • 1
  • 8
  • 14