【问题标题】:Cannot convert lambda expression to type 'string' because it is not a delegate type MVC无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型 MVC
【发布时间】:2014-05-17 03:06:54
【问题描述】:

我知道有很多这样的话题,但我找不到答案。

在代码中:

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>TabelaModel</legend>

    @Html.HiddenFor(model => model.ID)

    <div class="editor-label">
        @Html.LabelFor(model => model.Druzyna)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Druzyna)
        @Html.ValidationMessageFor(model => model.Druzyna);
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.LiczbaMeczy)
    </div>
    <div class="editor-field">
        @Html.TextBox(model => model.LiczbaMeczy)
        @Html.ValidationMessageFor(model => model.LiczbaMeczy)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.LiczbaGoliStrzelonych)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.LiczbaGoliStrzelonych)
        @Html.ValidationMessageFor(model => model.LiczbaGoliStrzelonych)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>

}

编译器正在抛出异常:

@Html.TextBox(model => model.LiczbaMeczy)

我已将 System.Linq、System.Data.Entity 放入模型、视图、控制器中,但仍然没有。当我将@Html.TextBox 替换为@Html.EditorFor 时它正在工作,但我真的很想避免这种情况。我的模型课:

namespace GridViewTest.Models
{
    public class TabelaModel
    {
        public int ID { get; set; }
        public string Druzyna { get; set; }
        [Required (ErrorMessage="*")]
        public string LiczbaMeczy { get; set; }
        [Required(ErrorMessage = "*")]
        public int LiczbaGoliStrzelonych { get; set; }
    }
}

你能帮帮我吗?

【问题讨论】:

    标签: asp.net-mvc lambda


    【解决方案1】:

    你需要使用TextBoxFor(不是TextBox):

    @Html.TextBoxFor(model => model.LiczbaMeczy)
    

    这将采用 lambda 表达式。

    【讨论】:

    • 谢谢您,它正在工作!我会在 10 分钟内接受答复:)。我确信@Html.TextBox 会对此有好处..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多