Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET HttpPost未将字典对象传递到操作方法_C#_Asp.net Mvc_Controller_Http Post - Fatal编程技术网

C# ASP.NET HttpPost未将字典对象传递到操作方法

C# ASP.NET HttpPost未将字典对象传递到操作方法,c#,asp.net-mvc,controller,http-post,C#,Asp.net Mvc,Controller,Http Post,在控制器代码中的HttPost方法中检索集合类时遇到问题。 例如,出于某种原因,它能够检索“int”,但不能检索字典 我在ASPX文件中使用了一个隐藏的输入标记。 我确实在ASPX文件和控制器页面中的HTTP post前后放置了Debug.WriteLine,但字典仅在ASPX页面中填充。对于我需要它的代码页,它是0 ...</tr> <% i++; %> <% list.Add(user.ID.ToS

在控制器代码中的HttPost方法中检索集合类时遇到问题。 例如,出于某种原因,它能够检索“int”,但不能检索字典

我在ASPX文件中使用了一个隐藏的输入标记。 我确实在ASPX文件和控制器页面中的HTTP post前后放置了Debug.WriteLine,但字典仅在ASPX页面中填充。对于我需要它的代码页,它是0

...</tr>
                <% i++; %>
                <% list.Add(user.ID.ToString()); %>
                <% list.Add(user.FirstName.ToString()); %>
                <% list.Add(user.LastName.ToString()); %>
                <% list.Add(user.Seniority.ToString()); %>
                <% list.Add(sec_a.ToString()); %>
                <% list.Add(sec_b.ToString()); %>
                <% list.Add(sec_c.ToString()); %>
                <% list.Add(uvid_a.ToString()); %>
                <% details.Add(i, list); %>
             <% } %>
         </tbody>
         </table>
          // it works here but not in the .CS file where the post is made
        <% System.Diagnostics.Debug.WriteLine("Before Post value: " + details.Count); %>
          // here is the hidden field that is supposed to send the variable into the Action method
        <input type="hidden" name="student_details" value="<%= details %>" />
        <div class="buttons">
<button type="submit" class="button">Export Data</button>
        </div>
    </fieldset>
    </form>
</article>
。。。
//它在这里工作,但不在发布文章的.CS文件中
//这是一个隐藏字段,它应该将变量发送到Action方法中
导出数据

//控制器文件
[HttpPost]
公共行动结果报告(字典学生详细信息)
{
var grid=new System.Web.UI.WebControls.GridView();
var datatable=新的datatable();
datatable.Columns.Add(“用户ID”);
datatable.Columns.Add(“名字”);
datatable.Columns.Add(“姓氏”);
datatable.Columns.Add(“xxx”);
datatable.Columns.Add(“yyy”);
datatable.Columns.Add(“zzz”);
datatable.Columns.Add(“标题”);
数据表。列。添加(“等级”);
System.Diagnostics.Debug.WriteLine(“在Post值之后:+student_details.Count”);
foreach(学生信息中的var螺柱)
{
datatable.Rows.Add(stud.Value);
}

它传递的是一个int变量,而不是一个字典。

为什么要这样做?将源于服务器端的数据传递到客户端,只是为了传递回来?而且您不能只将.NET Dictionary对象回显到HTML表单,并期望它再次转换为.NET Dictionary对象。查看源代码,默认情况下,您将看到
student\u details
=
System.Collections.Generic.Dictionary
2[System.Int32,System.String]`我明白了。明白了。我会在Controller类中做所有事情。谢谢。
    //Controller file    
    [HttpPost]
    public ActionResult Report(Dictionary<int, List<string>> student_details)
    {
        var grid = new System.Web.UI.WebControls.GridView();
        var datatable = new DataTable();

        datatable.Columns.Add("User ID");
        datatable.Columns.Add("First Name");
        datatable.Columns.Add("Last Name");
        datatable.Columns.Add("xxx");
        datatable.Columns.Add("yyy");
        datatable.Columns.Add("zzz");
        datatable.Columns.Add("Title");
        datatable.Columns.Add("Grade");

        System.Diagnostics.Debug.WriteLine("After Post value: " + student_details.Count);

        foreach (var stud in student_details)
        {
            datatable.Rows.Add(stud.Value);
        }