[英]Get parameter from view to controller
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: 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. 问题是我没有在控制器中获取“清单”值,所以它为
null
所以我无法使用它。 Can anyone explain me why it happens? 谁能解释我为什么会这样? Regards
问候
Try to execute the anonymous function where you did put the ajax request 尝试在放置了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);
}
}
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! 我希望这有帮助!
Please use following code in Javascript 请在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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.