【问题标题】:How to solve System.ArgumentException: An item with the same key has already been added in mvc 3?如何解决 System.ArgumentException:在 mvc 3 中已经添加了具有相同键的项目?
【发布时间】:2012-07-09 11:20:19
【问题描述】:

我创建了 ViewModel 来测试这个答案:How to fill ViewModel which has List<T> property after clicking submit button mvc 3?

我需要复选框列表,请查看 WeeklyModel.Also WeeklyViewModel。我想在复选框中列出 alldoays。在下面说我的错误。这是愚蠢的错误。我无法理解 reson to solve 怎么解决呢?谢谢... 看看请帮忙(我需要什么)文章:When do I use View Models, Partials, Templates and handle child bindings with MVC 3 型号:


   public class WeeklyModel

        {
            public string Name { get; set; }
            public string Value { get; set; }
            public bool IsChecked { get; set; }
        }


       public class WeeklyViewModel
        {
            public IEnumerable<WeeklyModel> Settings { get; set; }
            public WeeklyViewModel()
            {
                Settings = new List<WeeklyModel>();
            }

        }

控制器:


 public ActionResult CreateWeekly()
       {
           var model = new WeeklyViewModel();
            List<WeeklyModel> li = new List<WeeklyModel>();
            li.Add( new WeeklyModel(){ Name="Monday", Value="mon", IsChecked=false});
            model.Settings = li;
               return View(model);
       }
       [HttpPost]
        public ActionResult CreateWeekly( WeeklyViewModel weekly)
       {

           return View("CreateWeekly", weekly);
       }

我创建了 Views/Trigger/EditorTemplates/WeeklyModels.aspx:


<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ChildSite.Master" Inherits="System.Web.Mvc.ViewPage<GenSystem.Models.WeeklyModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%:Html.CheckBoxFor(m=>m.IsChecked) %>
<%:Html.LabelFor(m=>m.IsChecked,Model.Name) %>
<%:Html.HiddenFor(m=>m.Name) %>
<%:Html.HiddenFor(m=>m.Value) %>

</asp:Content>

查看:


<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ChildSite.Master" Inherits="System.Web.Mvc.ViewPage<GenSystem.Models.WeeklyViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    CreateWeekly
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<% 

    using (Html.BeginForm())
    {
          %>

      <div>
     <%:Html.EditorFor(m => m.Settings)%>
      </div>
    <br />
       <input value="GenerateForWeekly" name="submitButton" type="submit" />
          <%} %>
</asp:Content>

   

如何解决这个错误:

System.ArgumentException:已添加具有相同键的项目。 在 System.ThrowHelper.ThrowArgumentException(ExceptionResource 资源) 在 System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary2.Add(TKey 键,TValue 值) 在 Transformer.NET.Token.ParseAnchors() 在 Transformer.NET.TextTransformer.Parse(List1 tokensType, Dictionary2 变量) 在 Transformer.NET.TextTransformer.Transform(List1 tokensType, Dictionary2 变量) 在 Transformer.NET.TextTransformer.Transform(List`1 tokensType) 在 Transformer.NET.TextTransformer.Transform() 在 Ext.Net.ExtNetTransformer.Transform(字符串文本) 在 Ext.Net.InitScriptFilter.Transform() 在 Ext.Net.InitScriptFilter.Flush() 在 System.Web.HttpWriter.Filter(布尔 finalFiltering) 在 System.Web.HttpResponse.FilterOutput() 在 System.Web.HttpApplication.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3 model-view-controller


    【解决方案1】:

    编辑器模板应该是.ascx 控件,而不是.aspx 页面。此外,您的文件命名错误。它被命名为WeeklyModels.aspx,但正确的名称是WeeklyModel.ascx(没有s),因为你的类被称为WeeklyModel而不是WeeklyModels

    所以在~/Views/Shared/EditorTemplates/WeeklyModel.ascx 中,你可以输入以下内容:

    <%@ Control 
        Language="C#" 
        Inherits="System.Web.Mvc.ViewUserControl<GenSystem.Models.WeeklyModels>" 
    %>
    <%= Html.CheckBoxFor(m => m.IsChecked) %>
    <%= Html.LabelFor(m => m.IsChecked, Model.Name) %>
    <%= Html.HiddenFor(m => m.Name) %>
    <%= Html.HiddenFor(m => m.Value) %>
    

    在你的主视图中CreateWeekly.aspx:

    <%@ Page 
        Title="" 
        Language="C#" 
        MasterPageFile="~/Views/Shared/ChildSite.Master" 
        Inherits="System.Web.Mvc.ViewPage<GenSystem.Models.WeeklyViewModel>" 
    %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        CreateWeekly
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <% using (Html.BeginForm()) { %>
            <div>
                <%= Html.EditorFor(m => m.Settings) %>
            </div>
            <br />
            <input value="GenerateForWeekly" name="submitButton" type="submit" />
        <% } %>
    </asp:Content>
    

    【讨论】:

    • 感谢工作,但有点问题:" %> 为什么要绘制蓝色下划线和say : 语句不能出现在方法体之外 /multiline lambda
    • 再次感谢。此 ascx 没有蓝色下划线:
    猜你喜欢
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    相关资源
    最近更新 更多