我从ajax发送FileHeadNo到我的控制器动作,但它没有接收FileHeadNo。
$(function () {
$("#txtReportNo").autocomplete({
source: function (request, response) {
var reportNo = $("#txtReportNo").val();
var FileHeadNo = $("#txtFileHeadNo").val();
var InitialsID = ""; //$("#CNGInspectionReport_InitialsID").val();
var ProjectID = ""; //$("#CNGInspectionReport_ProjectID").val();
var url;
var data;
var onlyReport = 0;
// console.log(InitialsID + " " + ProjectID);
//if (($("#CNGInspectionReport_InitialsID").val() == null || $("#CNGInspectionReport_InitialsID").val() == "" || $("#CNGInspectionReport_InitialsID").val() == "0") && ($("#CNGInspectionReport_ProjectID").val() == "" || $("#CNGInspectionReport_ProjectID").val() == "0")) {
url = "/CNGInspectionReport/GetInspectionReportFilteredIssuedOnly"
reportNo = { reportNo: $("#txtReportNo").val() };
data = JSON.stringify(reportNo);
onlyReport = 1;
if (onlyReport == 1) {
data = JSON.stringify(reportNo);
}
else {
var Listdata = { reportNo: $("#txtReportNo").val(), FileHeadNo: $("#txtFileHeadNo").val() };
data = JSON.stringify(Listdata);
}
$.ajax({
url: url,
data: data,
dataType: "json",
type: "POST",
cache: false,
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
//debugger;
//response(JSON.parse(data));
response($.map(data.result, function (item) {
return {
value: item //item.VelosiReportNo
}
}))
},
//error: function (XMLHttpRequest, textStatus, errorThrown) {
// var err = eval("(" + XMLHttpRequest.responseText + ")");
// alert(err.Message)
//}
});
},
minLength: 1 //This is the Char length of inputTextBox
});
});
这是一个动作方法,但是它接收的FileHeadNo为空。
public ActionResult GetInspectionReportFilteredIssuedOnly(string reportNo, string FileHeadNo= "")
{
try
{
我试了所有的方法,但它是空的。请在这件事上帮帮我。
代码:
url = "/CNGInspectionReport/GetInspectionReportFilteredIssuedOnly"
reportNo = { reportNo: $("#txtReportNo").val() };
data = JSON.stringify(reportNo);
onlyReport = 1;
if (onlyReport == 1) {
data = JSON.stringify(reportNo);
}
else {
var Listdata = { reportNo: $("#txtReportNo").val(), FileHeadNo: $("#txtFileHeadNo").val() };
data = JSON.stringify(Listdata);
}
替换为:
url = "/CNGInspectionReport/GetInspectionReportFilteredIssuedOnly"
var data;
if (onlyReport == 1) {
data = {reportNo:reportNo, FileHeadNo:""};
}
else {
data = {reportNo:reportNo, FileHeadNo:FileHeadNo};
}
从ajax中删除以下代码:
dataType: "json",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
和添加[FromBody]到你的动作头:
public ActionResult GetInspectionReportFilteredIssuedOnly([FromBody] string reportNo, string FileHeadNo= "")
我将给出一个简单的例子,试着将你的代码与它匹配
ajax请求:
<script>
$('.addtobasket').click(function () {
var productid = $(this).attr('productid');
var count = $(this).attr('count');
$.ajax({
url: "/Basket/AddToBasket",
data: { productid : productid , count : count },
type: "Post",
dataType: "Json",
success: function (result) {
if(result.Success)
{
$("#framebasket").html(result.HtmlBody);
}
else
{
alert(result.HtmlMsg);
}
},
error: function () {
alert("error");
}
});
});
</script>
控制器类
[HttpPost]
public ActionResult AddToBasket(int productid, int count)
{
//add to basket......
return Json(new JsonData()
{
HtmlMsg = "Item Add to Your Basket",
HtmlBody = this.RenderPartialToString("_BasketList", model),
Success = true,
});
}
html辅助类
public static class ViewHelper
{
public static string RenderPartialToString(this ControllerBase controller, string partialViewName, object model)
{
IView view = ViewEngines.Engines.FindPartialView(controller.ControllerContext, partialViewName).View;
return RenderViewToString(controller, view, model);
}
}
JsonData类
public class JsonData
{
public string HtmlMsg { get; set; }
public string HtmlBody { get; set; }
public bool Success { get; set; }
}
我希望这是有用的
请看下面的代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>autocomplete demo</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<label for="autocomplete">Select a programming language: </label>
<input id="autocomplete">
<script>
var tags = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ];
$( "#autocomplete" ).autocomplete({
source: function( request, response ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( tags, function( item ){
return matcher.test( item );
}) );
}
});
</script>
</body>
</html>