How to solve System.ArgumentException: An item with the same key has already been added in mvc 3?

i created ViewModel to test this answer : How to fill ViewModel which has List<T> property after clicking submit button mvc 3?

i need checkboxlist look please WeeklyModel.Also WeeklyViewModel. i want to list alldoays in checkboxes. Nut my error below . it is stupid error. i can not understand reson to solve how to solve it? thanks... Look please helpful ( what i need artcile) article : When do I use View Models, Partials, Templates and handle child bindings with MVC 3 Models:

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

        }

Controller:

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

i created 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>

View :

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

   

How to solve this Error:

System.ArgumentException: An item with the same key has already been added. at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary2.Add(TKey key, TValue value) at Transformer.NET.Token.ParseAnchors() at Transformer.NET.TextTransformer.Parse(List1 tokensType, Dictionary2 variables) at Transformer.NET.TextTransformer.Transform(List1 tokensType, Dictionary2 variables) at Transformer.NET.TextTransformer.Transform(List`1 tokensType) at Transformer.NET.TextTransformer.Transform() at Ext.Net.ExtNetTransformer.Transform(String text) at Ext.Net.InitScriptFilter.Transform() at Ext.Net.InitScriptFilter.Flush() at System.Web.HttpWriter.Filter(Boolean finalFiltering) at System.Web.HttpResponse.FilterOutput() at System.Web.HttpApplication.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Answers


Editor templates should be .ascx controls, not .aspx pages. Also your file is wrongly named. It is named WeeklyModels.aspx but the correct name is WeeklyModel.ascx (without s) because your class is called WeeklyModel and not WeeklyModels.

So inside ~/Views/Shared/EditorTemplates/WeeklyModel.ascx you could put the following:

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

and inside your main view 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>

Need Your Help

How to filter a product is in multiple category?

javascript jquery

The objective is, I have some products and each product is under multiple categories. Initially I list all product and I need to filter by jQuery. I've tried the following method but it seems not w...

The right way to use postgresql with concurrent processes

python postgresql psycopg2

I am accessing a postgresql table from Python with psycopg2. I am doing this from several processes. I've been using serialization transaction isolation to maintain the integrity of the data. I do