0
votes

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

2
You wrote @Html.TextBox instead of @Html.TextBoxFor note the For?? - Callum Linington
@ 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
@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
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
@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" } - user3559349

2 Answers

3
votes

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

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