【发布时间】: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