0

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

我需要复选框列表,请查看 WeeklyModel.Also WeeklyViewModel。我想在复选框中列出 alldoays。在下面说我的错误。这是愚蠢的错误。我无法理解 reson to solve 怎么解决呢?谢谢...请看有帮助(我需要什么)文章:我何时使用视图模型、部分、模板并使用 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.Dictionary 2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary2.Add(TKey key, TValue value) 在 Transformer.NET.Token.ParseAnchors() 在 Transformer.NET.TextTransformer.Parse(列出1 tokensType, Dictionary2 个变量) 在 Transformer.NET.TextTransformer.Transform(List1 tokensType, Dictionary2 个变量)在 Transformer.NET.TextTransformer.Transform(List`1 tokensType) 在 Transformer.NET.TextTransformer.Transform() 在 Ext.Net.ExtNetTransformer.Transform(String text) 在 Ext.Net.InitScriptFilter.Transform() 在Ext.Net.InitScriptFilter.Flush() 在 System.Web.HttpWriter.Filter(Boolean finalFiltering) 在 System.Web.HttpResponse.FilterOutput() 在 System.Web.HttpApplication.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute( ) 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤,Boolean& completedSynchronously)

4

1 回答 1

2

编辑器模板应该是.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>
于 2012-07-09T11:25:01.280 回答