英文:
To bring the selected records from the table to the controller Get method
问题
我试图从Javascript函数EditbreakDown中的选定行中获取所有列值,例如'RegNo','Customer','FleetDate',这些值需要传递到具有HttpGet方法的方法。如果将HttpPost更改为HttpGet,那么来自Javascript的值将不会传递。有人可以告诉我如何将这些值传递到控制器SummaryReport的HttpGet方法中。以下是代码:
public class FleetSummViewModel
{
public string CustomerCode { get; set; }
public string Depot { get; set; }
public string FleetNo { get; set; }
public string RegNo { get; set; }
public string FleetDate { get; set; }
}
[HttpGet]
public IActionResult SummaryReport(List<FleetSummViewModel> fleets)
{
}
<button type="button" name="submitButton" value="Edit" class="btn btn-primary form-control" onclick="EditbreakDown()">
Edit
</button>
function EditbreakDown() {
var fleets = []; // 创建一个空数组以保存报告对象
var selectedRows = table.rows('.selected').nodes(); // 获取选定行节点的数组
$(selectedRows).each(function() {
var fleet = {}; // 为每个选定的行创建一个新的报告对象
fleet.CustomerCode = table.cell(this, 0).data(); // 将列数据分配给报告对象
fleet.RegNo = table.cell(this, 2).data();
fleet.FleetNo = table.cell(this, 3).data();
fleet.FleetDate = table.cell(this, 4).data();
fleets.push(fleet);
})
$.ajax({
type: "GET",
//contentType: "application/json; charset=utf-8",
url: "/FleetCheck/SummaryReport",
//dataType: "json",
data: {fleets: fleets},
success: function (response) {
console.log("dfgdfg");
},
error: function (response) {
var misinfo = ('@ViewBag.Result');
console.log(misinfo);
}
});
}
请注意,你需要确保在JavaScript代码中的table
变量是定义的,以及路由地址"/FleetCheck/SummaryReport"
是正确的。这段代码试图使用GET请求将选定行的数据传递到SummaryReport
方法。
英文:
I am trying to bring all the column values like 'RegNo','Customer', 'FleetDate' from the selected rows of the table from the Javascript function EditbreakDown. Currently selected values is passed only if the controller method is HttpPost method. I need to bring all the above value to the method having HttpGet. If I change HttpPost into HttpGet, the value from Javascript will not be passed. Someone can help me how can bring to the HttpGet method in controller SummaryReport. Here is the code
public class FleetSummViewModel
{
public string CustomerCode { get; set; }
public string Depot { get; set; }
public string FleetNo { get; set; }
public string RegNo { get; set; }
public string FleetDate { get; set; }
}
[HttpGet]
public IActionResult SummaryReport(List<FleetSummViewModel> fleets)
{
}
<button type="button" name="submitButton" value="Edit" class="btn btn-primary form-control"
onclick="EditbreakDown()">
Edit
</button>
function EditbreakDown() {
var fleets = []; // create an empty array to hold the report objects
var selectedRows = table.rows('.selected').nodes(); // get an array of the selected row nodes
$(selectedRows).each(function() {
var fleet = {}; // create a new report object for each selected row
fleet.CustomerCode = table.cell(this, 0).data(); // assign the column data to the report object
fleet.RegNo = table.cell(this, 2).data();
fleet.FleetNo = table.cell(this, 3).data();
fleet.FleetDate = table.cell(this, 4).data();
fleets.push(fleet);
})
$.ajax({
type: "GET",
//contentType: "application/json; charset=utf-8",
url: "/FleetCheck/SummaryReport",
//dataType: "json",
data: {fleets:fleets},
success: function (response) {
console.log("dfgdfg");
},
error: function (response) {
var misinfo = ('@ViewBag.Result');
console.log(misinfo);
}
});
}
答案1
得分: 0
> 目前仅在控制器方法为HttpPost方法时传递所选的值。我需要将上述值全部传递给使用HttpGet的方法。如果我将HttpPost更改为HttpGet,来自Javascript的值将不会传递。有人可以告诉我如何将其传递给控制器SummaryReport的HttpGet方法吗。
通常,要在控制器中发送列表类型的对象,HttpPost方法通常是一个很好的选择,考虑所有方面。但是,如果您坚决要通过HttpGet发送它,这不是一个好的做法,但您仍然可以使用HttpGet发送请求。
为了实现这一点,您需要将 fleets 对象序列化,同时您需要将字符串参数设置为控制器参数。一旦您接收到作为控制器参数传递的值,那么您需要将该字符串反序列化为您的 List 类。
让我们在操作中检查一下。
控制器:
[HttpGet]
public IActionResult SummaryReport(string fleets)
{
List<FleetSummViewModel> retrieveObject = JsonConvert.DeserializeObject<List<FleetSummViewModel>>(fleets);
return Ok(retrieveObject.Take(3));
}
HTML:
<button type="button" name="submitButton" value="Edit" class="btn btn-primary form control" onclick="EditbreakDown()">
Edit
</button>
Javascript:
@section scripts {
<script>
function EditbreakDown() {
alert("Working");
var fleets = [];
for (let i = 1; i <= 10; i++) {
var items = {
CustomerCode: "CustomerCode:" + i,
RegNo: "RegNo:" + i,
FleetNo: "FleetNo:" + i,
}
fleets.push(items);
}
console.log(fleets);
var json = JSON.stringify(fleets);
var url = '@Url.Action("SummaryReport","FleetCheck")';
$.ajax({
url: url,
type: "GET",
dataType: "JSON",
data: { fleets: json },
success: function (response) {
console.log(response);
}
});
}
</script>
}
注意: 在这里,我使用了自定义对象来排队 fleets 数组。在您的情况下,您将用您的 var selectedRows = table.rows('.selected').nodes()
表行替换它,然后将对象序列化为字符串。
输出:
注意: 请注意,HttpGet 有请求限制。一旦超过限制,您可能会遇到空请求,或者请求过滤可能会阻止请求,因为请求超过了请求限制,这可能会导致出现404.15错误代码。在我们的官方文档中可以找到更多信息。
英文:
> Currently selected values is passed only if the controller method is
> HttpPost method. I need to bring all the above value to the method
> having HttpGet. If I change HttpPost into HttpGet, the value from
> Javascript will not be passed. Someone can help me how can bring to
> the HttpGet method in controller SummaryReport.
Generally, to send list kind of object in controller HttpPost would always be the good option considering all the aspects. But as you are determind to send it through HttpGet; Thus, this won't be good practice but you can still send request using HttpGet.
To achieve that, you have to stringify your fleets object and at the same time you need to set string parameter as of your controller argument. Once you would receive the value passed as your controller argument then you would require to Deserialize that string into your List class.
Let's check that in action.
Controller:
[HttpGet]
public IActionResult SummaryReport(string fleets)
{
List<FleetSummViewModel> retrieveObjeect = JsonConvert.DeserializeObject<List<FleetSummViewModel>>(fleets);
return Ok(retrieveObjeect.Take(3));
}
HTML:
<button type="button" name="submitButton" value="Edit" class="btn btn-primary form-control"
onclick="EditbreakDown()">
Edit
</button>
Javascript:
@section scripts {
<script>
function EditbreakDown() {
alert("Working");
var fleets = [];
for (let i = 1; i <= 10; i++) {
var items = {
CustomerCode: "CustomerCode:" + i,
RegNo: "RegNo:" + i,
FleetNo: "FleetNo:" + i,
}
fleets.push(items);
}
console.log(fleets);
var json = JSON.stringify(fleets);
console.log(json);
var url = '@Url.Action("SummaryReport","FleetCheck")';
$.ajax({
url: url,
type: "GET",
datatype: "JSON",
data: { fleets: json },
success: function (response) {
console.log(response);
}
});
}
</script>
}
Note: Here, I am using my self defined object to enqueue the fleets array. In your scenario, you would replace this with your var selectedRows = table.rows('.selected').nodes()
table rows. then stringify the object.
Output:
Note: Please be informed that, HttpGet has the request limit. Once it exceeds the limit you may encounter null request or request filtering can blocks the request as the request exceeds the request limits which may through 404.15 error code as well. More can be found here in our official document.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论