简体   繁体   English

ASP.NET MVC:将复杂的数据从视图传递到控制器

[英]ASP.NET MVC: Pass complex data from View to Controller

I have a Controller that generates the following object and passes it to the View via the ViewBag: 我有一个控制器,它生成以下对象并将其通过ViewBag传递给View:

public class ObjectList
{
  public ObjectItem[] Item { get; set; }
}


public class ObjectItem
{
  public string Name { get; set; }
  public decimal Value1 { get; set; }
  public decimal Value2 { get; set; }
}

In the view this data is displayed in a table. 在视图中,该数据显示在表格中。 Blow the table I have an ActionLink that should pass all the data back to the Controller for further processing. 删除表我有一个ActionLink,该ActionLink应该将所有数据传递回Controller进行进一步处理。 How can I do this. 我怎样才能做到这一点。 Is a ActionLink the right choice, it can be hundreds of OjectItems in the List. 是ActionLink的正确选择,它可以是List中的数百个OjectItem。 Is there any other approach for this? 还有其他方法吗?

I hope you can help me with this. 希望您能帮到我。

Not sure if there's a way to do it with MVC, but what i normally do is use jquery to go through the items in the table, push it to a javascript array, and then pass the array as parameter when making request to an MVC controller. 不知道是否有办法使用MVC,但是我通常要做的是使用jquery遍历表中的项目,将其推送到javascript数组,然后在向MVC控制器发出请求时将数组作为参数传递。

For example: 例如:

// Javascript variable that will contain the values
var selectedPositionNumbers = new Array();

// In my case, i store the value of the row when user click a checkbox in the same row
$('body').on('click', 'input[id*="IsSelected"]', function () {
            var positionNr = $(this).attr('id').split("IsSelected")[1];
            var isChecked = $(this).is(':checked');
            if (isChecked) {
                if ($.inArray(positionNr, selectedPositionNumbers) == -1) {
                    selectedPositionNumbers.push(positionNr);
                }
            }
        });

// And later on when making the request
$.ajax({
        url: '@Url.Action("SendSelectedValues")',
        data: JSON.stringify({ positionNrList: selectedPositionNumbers }),
        type: 'post',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            // Completed statement here
        }
    });

In my case i only need to store one value so i just have to make a string array, but for your case, maybe this article gives a better example: http://abiyh.blogspot.nl/2011/01/posting-javascript-array-using-jquery.html 就我而言,我只需要存储一个值,所以我只需要创建一个字符串数组,但是对于您而言,本文可能会提供一个更好的示例: http : //abiyh.blogspot.nl/2011/01/posting-javascript -array-using-jquery.html

If the information is of any critical value, I would definitely save a copy of it on the server (database, file system, etc.) and have the ActionLink submit some kind of id back to the server to help retrieve it. 如果该信息具有任何重要价值,我肯定会将其副本保存在服务器(数据库,文件系统等)上,并让ActionLink将某种ID提交回服务器以帮助检索它。 You would need to clean "unused" data (eg that was never exported and is no longer needed), but depending on your situation, it might be safer than risking user tempering with data that you think you have sent out. 您将需要清除“未使用”的数据(例如,从未导出过且不再需要的数据),但是根据您的情况,这可能比冒着用户认为您已发送的数据受到用户控制的风险更安全。

I think you should store the data in the session. 我认为您应该将数据存储在会话中。 When the action link posts back, you would just grab the version of the data that is stored in the session. 当操作链接回发时,您只需获取存储在会话中的数据的版本。

As with the answer provided by @JTMon, if you use the action link to send the data back to the server you have to validate all the data (as it is now user input and could have been tampered with). 与@JTMon提供的答案一样,如果使用操作链接将数据发送回服务器,则必须验证所有数据(因为它现在是用户输入的,可能已被篡改)。 You could use mechanisms such as a CRC or HMAC check. 您可以使用诸如CRC或HMAC检查的机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM