将所选记录从表格传递到控制器的Get方法。

huangapple go评论60阅读模式
英文:

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&lt;FleetSummViewModel&gt; fleets)
 {
 }

 
&lt;button type=&quot;button&quot; name=&quot;submitButton&quot; value=&quot;Edit&quot; class=&quot;btn btn-primary form-control&quot;
         onclick=&quot;EditbreakDown()&quot;&gt;
         Edit
&lt;/button&gt;
                        
function EditbreakDown() {
        var fleets = []; // create an empty array to hold the report objects
        var selectedRows = table.rows(&#39;.selected&#39;).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: &quot;GET&quot;,
            //contentType: &quot;application/json; charset=utf-8&quot;,
            url: &quot;/FleetCheck/SummaryReport&quot;,
            //dataType: &quot;json&quot;,
            data: {fleets:fleets},
            success: function (response) {
                console.log(&quot;dfgdfg&quot;);
            },
            error: function (response) {
                var misinfo = (&#39;@ViewBag.Result&#39;);
                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() 表行替换它,然后将对象序列化为字符串。

输出:

将所选记录从表格传递到控制器的Get方法。

注意: 请注意,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&lt;FleetSummViewModel&gt; retrieveObjeect = JsonConvert.DeserializeObject&lt;List&lt;FleetSummViewModel&gt;&gt;(fleets);
            return Ok(retrieveObjeect.Take(3));
        }

HTML:

&lt;button type=&quot;button&quot; name=&quot;submitButton&quot; value=&quot;Edit&quot; class=&quot;btn btn-primary form-control&quot;
        onclick=&quot;EditbreakDown()&quot;&gt;
    Edit
&lt;/button&gt;

Javascript:

@section scripts {
    &lt;script&gt;
        function EditbreakDown() {

            alert(&quot;Working&quot;);

            var fleets = [];
            for (let i = 1; i &lt;= 10; i++) {
                var items = {
                    CustomerCode: &quot;CustomerCode:&quot; + i,
                    RegNo: &quot;RegNo:&quot; + i,
                    FleetNo: &quot;FleetNo:&quot; + i,

                }
                fleets.push(items);

               
            }

            console.log(fleets);

            var json = JSON.stringify(fleets);

            console.log(json);
            var url = &#39;@Url.Action(&quot;SummaryReport&quot;,&quot;FleetCheck&quot;)&#39;;
            $.ajax({
                url: url,
                type: &quot;GET&quot;,
                datatype: &quot;JSON&quot;,
                data: { fleets: json },
                success: function (response) {
                    console.log(response);
                }
            });
        }
    &lt;/script&gt;
}

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(&#39;.selected&#39;).nodes() table rows. then stringify the object.

Output:

将所选记录从表格传递到控制器的Get方法。

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.

huangapple
  • 本文由 发表于 2023年2月27日 05:23:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定