0

I know these question(s) has been asked

  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

But none of them has helped me to find the problem that i am facing

i have tried to use this solution

<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>

to make one edit field read only, but i do get an error of Cannot convert lambda expression to type 'Delegate' because it is not a delegate type i do use entity frame work and these using

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;

the suggestion on other questions are that using

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

should fix the problem but it does not work for me I am using MVC 5

i have fixed the issue by using this solution

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

but because i am new on MVC and entity frame work i want learn why the other way throw an error

Sorry for English "mistake" Thank you

Community
  • 1
  • 1
Reza Del
  • 763
  • 10
  • 30
  • 2
    You wrote `@Html.TextBox` instead of `@Html.TextBoxFor` note the `For`?? – Callum Linington Jul 07 '16 at 10:52
  • @ Callum Linington You are right For was my mistake But 1) when i use for it make the text box really ugly (I know i can use CSS to change the appearance of text box), 2) the update will not get the value of the text box and change the value to null – Reza Del Jul 07 '16 at 10:59
  • @RezaDel So long as you are using `@Html.TextBoxFor` as Callum suggested, then the DefaultModelBinder should assign the value correctly in your POST. – Geoff James Jul 07 '16 at 11:02
  • Also, `@Html.TextBox` and `@Html.TextBoxFor` shouldn't render any differently in HTML. They will both have the `name` attribute to whatever you set as the first argument. Difference between the 2 is one lets you type a `name` manually, the other uses the lambda expression to set the `name` – Geoff James Jul 07 '16 at 11:04
  • @Geoff James i have tried the answer from Callum and it does get ride of an error but on the update the value is updated to NULL – Reza Del Jul 07 '16 at 11:05
  • 1
    @RezaDel, The reason your get it as `null` is because you have disabled it (disabled controls do not submit a value) - make it just `new { @readonly = "readonly" }` –  Jul 07 '16 at 11:07
  • Good spot @StephenMuecke! Hayfever getting the best of my reading abilities – Geoff James Jul 07 '16 at 11:08
  • @Stephen Muecke Thank you very much you are right the disabled was the issue. @ Callum Linington if you add your comment as an answer i will accept it as a correct answer, Thank you all – Reza Del Jul 07 '16 at 11:10
  • @RezaDel I added my comment and stephens as answer for you – Callum Linington Jul 07 '16 at 14:02

2 Answers2

3

So your original code:

<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>

You should change it to:

<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>

Note, the addition of For on the TextBox and the removal of disabled = "disabled"

The reason for removing disabled as per @StephenMuecke

disabled controls do not submit a value

The reason for adding For is that it's extension method signature has the first parameter of an expression

Callum Linington
  • 14,213
  • 12
  • 75
  • 154
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.TextBoxFor(model => model.Postcode_area_name, new { @readonly = "readonly" })
</div>

As you are using tightly bound entities

Nikhil Ghuse
  • 1,258
  • 14
  • 31