我正在尝试使用Steve Sanderson's blog post来编辑可变长度列表。我已经通过NuGet包管理器安装了dll,并确保命名空间位于Views/web.config
文件中。但是,当我尝试编写using
语句时出现以下错误。
System.Web.Mvc.HtmlHelper<Monet.Models.AgentTransmission> does not contain a definition
for 'BeginCollectionItem' and no extension method 'BeginCollectionItem' accepting a first
argument of type 'System.Web.Mvc.HtmlHelper<Monet.Models.AgentTransmission>' could be
found (are you missing a using directive or an assmebly reference
查看/ Web.config中
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="HtmlHelpers.BeginCollectionItem" />
</namespaces>
部分视图(已更新)
@model Monet.Models.AgentRelationshipCodes
@using (Html.BeginCollectionItem("AgentRelationshipCodes"))
{
<tr>
<td>@Html.EditorFor(model => model.EffectiveDate, "NullableDate", new { @class = "relCodeDate2" })</td>
<td>@Html.EditorFor(model => model.RelationshipId, "NullableDate", new { @class = "relDistCode1", maxlength = 3 })</td>
@Html.HiddenFor(model => model.ID)
@Html.HiddenFor(model => model.RelCodeOrdinal)
</tr>
}
控制器(以防万一)
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Xml;
using Monet.MonetToDss;
using Monet.Common;
using Monet.Models;
using Monet.ViewModel;
using HtmlHelpers.BeginCollectionItem;
public ViewResult NewRelationshipCode()
{
return View("AddRelationshipCodePartial", new AgentRelationshipCodes());
}
答案 0 :(得分:12)
请尝试关闭并重新打开解决方案,以便编辑器选择更改。完成后我没有收到错误
System.Web.Mvc.HtmlHelper没有 包含&#39; BeginCollectionItem&#39;的定义没有延伸 方法&#39; BeginCollectionItem&#39;接受第一个类型的参数 &#39; System.Web.Mvc.HtmlHelper&#39;可能 找到了(你是否错过了使用指令或者非常适合的参考
答案 1 :(得分:3)
这是史蒂夫桑德森的第三方图书馆,您必须先从https://www.nuget.org/packages/BeginCollectionItem/安装:
Install-Package BeginCollectionItem
答案 2 :(得分:2)
我需要添加
import {formatDate} from '@angular/common';
formatDate(mydate.value.effDate, 'yyyyMMdd', 'en')
到Views文件夹的web.config中的名称空间。我的位于“区域”文件夹中,因此我需要在“视图”文件夹中添加它。
您也可以在视图上直接添加一个using语句,但是您必须记住将其添加到每个视图。
答案 3 :(得分:1)
这是一个黑暗中的刺,但你试过删除索引说明符[i]?据我所知,在使用BeginCollectionItem助手时你不应该需要一个。它自己生成唯一索引。
以下是我发现有用的帮助器的更多资源:
http://ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/ http://justmycode.blogspot.com/2012/07/learning-mvc-editing-variable-length.html
更新:参考提问者评论的例子
@model Monet.Models.AgentRelationshipCodes
@using (Html.BeginCollectionItem("AgentRelationshipCodes")) @*error displays here*@
{
<tr>
<td>@Html.EditorFor(m => Model.EffectiveDate, "NullableDate", new { @class = "relCodeDate2" })</td>
<td>@Html.EditorFor(m => Model.RelationshipId, "NullableDate", new { @class = "relDistCode1", maxlength = 3 })</td>
@Html.HiddenFor(m => Model.ID)
@Html.HiddenFor(m => Model.RelCodeOrdinal)
</tr>
}
答案 4 :(得分:0)
.Net Core
安装nuget软件包:-https://www.nuget.org/packages/BeginCollectionItemCore
,然后将其添加到 _ViewImports.cshtml :@using HtmlHelpers.BeginCollectionItemCore;