指定 TModel 时出错TextBoxFor 的 TProperty

发布于 2024-10-09 17:25:28 字数 2666 浏览 2 评论 0原文

我运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦境 2024-10-16 17:25:28

尝试这样:

<% using (Ajax.BeginForm(Model.CodeName + "FormPost", "Customer", null, new AjaxOptions() { HttpMethod = "POST" }, new { @class = "form-container" })) { %>
    <% foreach(var item in model.Items) { %>
        <%: Html.TextBoxFor(x => item.Link) %>
    <% } %>
<% } %>

或者更好地使用更干净的编辑器模板,因为您不再需要在视图中编写任何循环:

<% using (Ajax.BeginForm(Model.CodeName + "FormPost", "Customer", null, new AjaxOptions() { HttpMethod = "POST" }, new { @class = "form-container" })) { %>
    <%= Html.EditorFor(x => x.Items) %>
<% } %>

然后定义编辑器模板(~/Views/Shared/EditorTemplates/SocialNetworkItem.ascx< /代码>)

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<SocialNetworkItem>" %>
<%: Html.TextBoxFor(x => x.Link) %>

Try like this:

<% using (Ajax.BeginForm(Model.CodeName + "FormPost", "Customer", null, new AjaxOptions() { HttpMethod = "POST" }, new { @class = "form-container" })) { %>
    <% foreach(var item in model.Items) { %>
        <%: Html.TextBoxFor(x => item.Link) %>
    <% } %>
<% } %>

Or even better use an Editor Template which is much cleaner as you no longer need to write any loops in your view:

<% using (Ajax.BeginForm(Model.CodeName + "FormPost", "Customer", null, new AjaxOptions() { HttpMethod = "POST" }, new { @class = "form-container" })) { %>
    <%= Html.EditorFor(x => x.Items) %>
<% } %>

and then define the Editor Template (~/Views/Shared/EditorTemplates/SocialNetworkItem.ascx)

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<SocialNetworkItem>" %>
<%: Html.TextBoxFor(x => x.Link) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文