Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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# 向控制器提交字典_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 向控制器提交字典

C# 向控制器提交字典,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我的观点是强类型词典。我在foreach循环中使用Html.EditorFor(),该循环覆盖字典中的元素,并为值创建文本字段。当我试图提交它给我的表格时 [InvalidCastException: Specified cast is not valid.] System.Web.Mvc.CollectionHelpers.ReplaceDictionaryImpl(IDictionary`2 dictionary, IEnumerable`1 newContents) +95 [Ta

我的观点是强类型词典。我在foreach循环中使用Html.EditorFor(),该循环覆盖字典中的元素,并为值创建文本字段。当我试图提交它给我的表格时

[InvalidCastException: Specified cast is not valid.]
   System.Web.Mvc.CollectionHelpers.ReplaceDictionaryImpl(IDictionary`2 dictionary, IEnumerable`1 newContents) +95

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
在我的控制器中,我有:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SendDictionary()
{
   if (ModelState.IsValid )
    {
        Dictionary<int, int> dictionary = new Dictionary<int, int>();
        dictionary.Add(1, 1);       
dictionary.Add(2, 1);    
dictionary.Add(3, 1);    
dictionary.Add(4, 1);    
dictionary.Add(5, 1);         
        return View(dictionary);
     }
     else
     {
       return View();
     }
}


[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CallMe(Dictionary<int, int> Dict)
{
        if (ModelState.IsValid)
        {
            return View("YEs");
        }
        else
        {
            return View();
        }
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果SendDictionary()
{
if(ModelState.IsValid)
{
字典=新字典();
增加(1,1);
增加(2,1);
增加(3,1);
增加(4,1);
增加(5,1);
返回视图(字典);
}
其他的
{
返回视图();
}
}
[HttpPost]
[ValidateAntiForgeryToken]
公共行动结果调用(字典目录)
{
if(ModelState.IsValid)
{
返回视图(“是”);
}
其他的
{
返回视图();
}
}
型号:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MasterPage.Master" Inherits="System.Web.Mvc.ViewPage<Dictionary<int, int>>" %>

我认为:

 <% using (Html.BeginForm("CallMe", "Call", FormMethod.Post))
           {%>
        <%: Html.AntiForgeryToken()%>
        <%foreach (var key in Model.Keys)
          {%>
        <tr>
            <td>
                <%: Html.EditorFor(m => Model[key])%></td>
        </tr>
        <% } %>
        <tr>
            <td>
                <input type="submit" value="submit" /></td>
        </tr>
 <% } %>

型号[键]%>
有人能帮我解决这个错误吗??谢谢你试试这个

 Dictionary<object, object> dictionary = new Dictionary<object, object>();
Dictionary Dictionary=newdictionary();
试试这个

 Dictionary<object, object> dictionary = new Dictionary<object, object>();
Dictionary Dictionary=newdictionary();

在视图中,将您的html设置为以下类型:

@使用(Html.BeginForm(“CallMe”、“Call”、FormMethod.Post))
{
var列表=作为IDictionary的模型;
对于(var index=0;index
这样,在控制器中,您就可以

[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果调用(IDictionary dictionary)
{
//使用你的字典
var Dictionary 1=新字典();
字典1=(字典)字典;
if(ModelState.IsValid)
{
}
返回视图(字典1);
}

在视图中,将您的html设置为以下类型:

@使用(Html.BeginForm(“CallMe”、“Call”、FormMethod.Post))
{
var列表=作为IDictionary的模型;
对于(var index=0;index
这样,在控制器中,您就可以

[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果调用(IDictionary dictionary)
{
//使用你的字典
var Dictionary 1=新字典();
字典1=(字典)字典;
if(ModelState.IsValid)
{
}
返回视图(字典1);
}

在您的视图中,您可以显示
@model
位吗?我编辑它,以便您现在可以看到:)您需要说model[key].ToString()吗?我只是在猜测……只是试过了。它告诉我:模板只能与字段访问、属性访问、一维数组索引或单参数自定义索引器表达式一起使用在您的视图中,能否显示
@model
位请?我编辑它,以便您现在可以看到:)您需要说model[key].ToString()吗?我只是在猜测……只是试过了。它给了我:模板只能与字段访问、属性访问、一维数组索引或单参数自定义索引器表达式一起使用它没有给我一个例外,但在CallMe()中的字典中,只有两条记录。首先是key=controller和value=controller name。第二个是动作和动作名称。“controller”、“ControllerName”和“action”、“CallMe”没有给我一个例外,但是在CallMe()中的字典中,只有两条记录。首先是key=controller和value=controller name。第二个是动作和动作名称。“控制器”、“控制器名称”和“动作”、“呼叫我”