【问题标题】:Submitting Dictionary to Controller向控制器提交字典
【发布时间】:2013-10-06 06:17:03
【问题描述】:

我的视图是强类型字典。我在 foreach 循环中使用 Html.EditorFor() 遍历 Dictionary 中的元素并为值创建一个文本字段。当我尝试提交它给我的表单时

[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();
        }
}

型号:

<%@ 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>
 <% } %>

有人可以帮我解决这个错误吗?谢谢

【问题讨论】:

  • 在你看来,能不能把@model位给我看一下?
  • 我编辑了它,让你现在可以看到:)
  • 你需要说 Model[key].ToString() 吗?我只是猜测..
  • 刚试过。它给了我:模板只能与字段访问、属性访问、单维数组索引或单参数自定义索引器表达式一起使用

标签: c# asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

在视图中,使您的 html 像这种类型:

@using (Html.BeginForm("CallMe", "Call", FormMethod.Post))
{

  var list = Model as IDictionary<int, int>;

  for (var index = 0; index < Model.Count; index++)
  {
    <input type="text" name="dictionary[@index].Key" value="@list.Keys.ElementAtindex)" />
    <input type="text" name="dictionary[@index].Value" value="@list.Values.ElementAt(index)" />
  }
}

这样在控制器中你就可以得到

        [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CallMe(IDictionary<int, int> dictionary)
    {
        // Use your dictionary

        var dictionary1 = new Dictionary<int, int>();
        dictionary1 = (Dictionary<int, int>)dictionary;
        if (ModelState.IsValid)
        {
        }
        return View(dictionary1);
    }

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

    • 它并没有给我一个例外,但是在CallMe()中的Dictionary中,只有两条记录。首先是 key=controller 和 value=controller 名称。二是动作和动作名称。 “控制器”、“控制器名称”和“动作”、“CallMe”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多