指定 TModel 时出错TextBoxFor 的 TProperty
我运行 Items 集合并想要构建 TextBox,但出现以下错误:
'System.Web.Mvc.HtmlHelper<Web.Module>' does not contain a definition for
'TextBoxFor' and the best extension method overload
'System.Web.Mvc.Html.InputExtenstions.TextBoxFor<TModel,TProperty>System.Web.Mvc.HtmlHelper<TModel>,System.Linq.Expressions.Expression<System.Func<TModel, TProperty>>)' has some invalid arguments
我认为我的匿名函数是错误的,但为什么? 请帮助我
SocialNetworkModel 类
public class SocialNetworkModel : IViewModel
{
#region Properties
public List<SocialNetworkItem> Items { get; set; }
#endregion
public SocialNetworkModel(string param)
{
this.Create(param);
}
/// <summary>
/// DO NOT DELETE: default .ctor is used when binding posted data, from client, back to the model
/// </summary>
public SocialNetworkModel() { }
public void Create(string param)
{
Items = new List<SocialNetworkItem>();
foreach(var item in Customer.Current.GetSocialNetworkList())
{
Items.Add(new SocialNetworkItem
{
CodeName = item.CodeName,
Link = item.Link,
UserName = item.UserName,
Password = item.Password
});
}
}
}
public class SocialNetworkItem
{
#region Properties
[LocalizedDisplayName("CodeName", NameResourceType = typeof(Resources.Views.SocialNetwork))]
public string CodeName { get; set; }
[LocalizedDisplayName("Link", NameResourceType = typeof(Resources.Views.SocialNetwork))]
public string Link { get; set; }
[LocalizedDisplayName("UserName", NameResourceType = typeof(Resources.Views.SocialNetwork))]
public string UserName { get; set; }
[LocalizedDisplayName("Password", NameResourceType = typeof(Resources.Views.SocialNetwork))]
public string Password { get; set; }
#endregion
}
ViewUserControl
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Module>" %>
<% var model = Model.Model as SocialNetworkModel; %>
<div id="social-network-container">
<div id="social-network">
<% using (Ajax.BeginForm(Model.CodeName + "FormPost", "Customer", null, new AjaxOptions() { HttpMethod = "POST" }, new { @class = "form-container" }))
{ %>
<%model.Items.ForEach(item => { %>
<%: Html.TextBoxFor<SocialNetworkItem, string>(s => item.Link)%>
<%}); %>
<% }%>
</div>
谢谢
I run over Items collection and want to build TextBox but I get the following error:
'System.Web.Mvc.HtmlHelper<Web.Module>' does not contain a definition for
'TextBoxFor' and the best extension method overload
'System.Web.Mvc.Html.InputExtenstions.TextBoxFor<TModel,TProperty>System.Web.Mvc.HtmlHelper<TModel>,System.Linq.Expressions.Expression<System.Func<TModel, TProperty>>)' has some invalid arguments
I think my anonymous function is wrong, but why?
Please help me
SocialNetworkModel class
public class SocialNetworkModel : IViewModel
{
#region Properties
public List<SocialNetworkItem> Items { get; set; }
#endregion
public SocialNetworkModel(string param)
{
this.Create(param);
}
/// <summary>
/// DO NOT DELETE: default .ctor is used when binding posted data, from client, back to the model
/// </summary>
public SocialNetworkModel() { }
public void Create(string param)
{
Items = new List<SocialNetworkItem>();
foreach(var item in Customer.Current.GetSocialNetworkList())
{
Items.Add(new SocialNetworkItem
{
CodeName = item.CodeName,
Link = item.Link,
UserName = item.UserName,
Password = item.Password
});
}
}
}
public class SocialNetworkItem
{
#region Properties
[LocalizedDisplayName("CodeName", NameResourceType = typeof(Resources.Views.SocialNetwork))]
public string CodeName { get; set; }
[LocalizedDisplayName("Link", NameResourceType = typeof(Resources.Views.SocialNetwork))]
public string Link { get; set; }
[LocalizedDisplayName("UserName", NameResourceType = typeof(Resources.Views.SocialNetwork))]
public string UserName { get; set; }
[LocalizedDisplayName("Password", NameResourceType = typeof(Resources.Views.SocialNetwork))]
public string Password { get; set; }
#endregion
}
ViewUserControl
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Module>" %>
<% var model = Model.Model as SocialNetworkModel; %>
<div id="social-network-container">
<div id="social-network">
<% using (Ajax.BeginForm(Model.CodeName + "FormPost", "Customer", null, new AjaxOptions() { HttpMethod = "POST" }, new { @class = "form-container" }))
{ %>
<%model.Items.ForEach(item => { %>
<%: Html.TextBoxFor<SocialNetworkItem, string>(s => item.Link)%>
<%}); %>
<% }%>
</div>
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样:
或者更好地使用更干净的编辑器模板,因为您不再需要在视图中编写任何循环:
然后定义编辑器模板(
~/Views/Shared/EditorTemplates/SocialNetworkItem.ascx< /代码>)
Try like this:
Or even better use an Editor Template which is much cleaner as you no longer need to write any loops in your view:
and then define the Editor Template (
~/Views/Shared/EditorTemplates/SocialNetworkItem.ascx
)