0

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)

Community
  • 1
  • 1
loki
  • 2,926
  • 8
  • 62
  • 115

1 Answers1

2

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>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • thanks working but a little bit question: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> why drawing underline blue and say : Statement cannot appear outside of method body /multiline lambda – loki Jul 09 '12 at 11:40
  • Thanks again.This ascx not underline blue: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="GenSystem.Views.Shared.EditorTemplates.WebUserControl1" %> – loki Jul 09 '12 at 11:52