错误:无法将 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/