【问题标题】:Cannot convert lambda expression to type 'Delegate' because it is not a delegate type MVC 5无法将 lambda 表达式转换为类型“委托”,因为它不是委托类型 MVC 5
【发布时间】:2016-07-07 10:42:23
【问题描述】:

我知道有人问过这些问题

  1. Cannot convert lambda expression to type 'string' because it is not a delegate type
  2. Entity Framework - Cannot convert lambda expression to type 'string' because it is not a delegate type

但他们都没有帮助我找到我面临的问题

我已尝试使用此解决方案

<div class="form-group">
     @Html.LabelFor(model => model.Postcode_area_name, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
 @Html.TextBox(model => model.Postcode_area_name, new { disabled = "disabled", @readonly = "readonly" })
    </div>
</div>

使一个编辑字段只读,但我确实得到了一个错误 无法将 lambda 表达式转换为类型“委托”,因为它不是委托类型 我确实使用实体框架,而这些使用

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using CRM2.Models;

其他问题的建议是使用

using System.Data.Entity;
using System.Linq;

应该可以解决问题,但对我不起作用 我正在使用 MVC 5

我已经使用这个解决方案解决了这个问题

@Html.DisplayFor(model => model.Postcode_area_name)
@Html.HiddenFor(model => model.Postcode_area_name)

但是因为我是 MVC 和实体框架的新手,所以我想了解为什么其他方式会引发错误

对不起英文“错误” 谢谢你

【问题讨论】:

  • 你写的是@Html.TextBox而不是@Html.TextBoxFor注意For??
  • @Callum Linington 你是对的因为是我的错误但是 1)当我使用它时,让文本框真的很难看(我知道我可以使用 CSS 来改变文本框的外观),2)更新不会获取文本框的值,将值改为null
  • @RezaDel 只要您按照 Callum 的建议使用 @Html.TextBoxFor,那么 DefaultModelBinder 应该在您的 POST 中正确分配值。
  • 另外,@Html.TextBox@Html.TextBoxFor 在 HTML 中的呈现方式不应有任何不同。无论您设置为第一个参数,它们都将具有 name 属性。两者的区别是一个让你手动输入name,另一个使用lambda表达式设置name
  • @RezaDel,将其设为null 的原因是因为您已禁用它(禁用的控件不提交值) - 将其设为new { @readonly = "readonly" }

标签: c# asp.net-mvc entity-framework lambda


【解决方案1】:

所以你的原始代码:

<div class="form-group">
     @Html.LabelFor(model => model.Postcode_area_name, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
         @Html.TextBox(model => model.Postcode_area_name, new { disabled = "disabled", @readonly = "readonly" })
    </div>
</div>

你应该把它改成:

<div class="form-group">
     @Html.LabelFor(model => model.Postcode_area_name, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
         @Html.TextBoxFor(model => model.Postcode_area_name, new { @readonly = "readonly" })
    </div>
</div>

注意,在TextBox 上添加For 并删除disabled = "disabled"

根据@StephenMuecke 删除disabled 的原因

禁用的控件不提交值

添加For的原因是它的扩展方法签名有一个表达式的第一个参数

【讨论】:

    【解决方案2】:
      <div class="form-group">
     @Html.LabelFor(model => model.Postcode_area_name, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
     @Html.TextBoxFor(model => model.Postcode_area_name, new { @readonly = "readonly" })
    </div>
    

    当您使用紧密绑定的实体时

    【讨论】:

    • 它确实得到了一个错误,但我不知道为什么它不使用文本框中的数据更新文件并将其更新为 NULL
    • 你能分享一下你的控制器方法吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多