asp.net-mvc-4 - 错误 : Cannot convert lambda expression to type 'string' because it is not a delegate type

标签 asp.net-mvc-4

错误:无法将 lambda 表达式转换为类型“字符串”,因为它不是委托(delegate)类型

当我尝试在 mvc4 中添加 cshtml 页面时出现此错误。
在客户名称行:@Html.TextBox(m => Model.CustomerName)
谁能解释它的含义以及它为什么出现在这里?

代码是

@model DataEntryMvcApplication.Models.Customer
@using (Html.BeginForm())
{
    <p>Customer Name: @Html.TextBox(m => Model.CustomerName)</p>
    <p>ID:@Html.TextBox(m=>Model.CustomerId)</p>
    <input type="submit" name="Custtomer" />
}

这是模型类;
namespace DataEntryMvcApplication.Models
{
    public class Customer
    {
        public string CustomerId { get; set; }
        public string CustomerName { get; set; }
    }
}

最佳答案

您需要 Html.TextBoxFor 而不是 Html.TextBox:

@model DataEntryMvcApplication.Models.Customer
@using (Html.BeginForm())
{
    <p>Customer Name: @Html.TextBoxFor(m => m.CustomerName)</p>
    <p>ID:@Html.TextBoxFor(m => m.CustomerId)</p>
}

两者的区别解释here

关于asp.net-mvc-4 - 错误 : Cannot convert lambda expression to type 'string' because it is not a delegate type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22172510/

相关文章:

asp.net - 如何阻止 ASP.NET Web api PUT 动词返回 500

c# - 来自部分内部的 Asp.Net MVC 父 Html.ViewBag

c# - 如何在 <img> src 标签中连接两个字符串?

c# - Angular ASP.NET MVC 绑定(bind)

c# - 如果复杂对象具有从字符串隐式转换,则 QueryString 参数是否可以绑定(bind)到复杂对象

c# - ASP.NET Web Api - Startup.cs 不存在

asp.net-mvc-4 - 使用CustomErrors使用ResponseRewrite删除aspxerrorpath参数

c# - 将字符串转换为匿名参数,以从 ActionLink 方法发送到对象 RouteValues (MVC4)

asp.net-mvc - ASP.NET MVC4 在错误区域搜索 Controller

c# - 你如何使用 MVC/Razor 封装 logic/html