5

Error:Cannot convert lambda expression to type 'string' because it is not a delegate type

I am getting this error when I am trying to add in cshtml page in mvc4. at line

Customer Name: @Html.TextBox(m => Model.CustomerName)

Could anyone explain what is its meaning and why it comes here?

Code is

@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" />
}

and this is model class;

namespace DataEntryMvcApplication.Models
{
    public class Customer
    {
        public string CustomerId { get; set; }
        public string CustomerName { get; set; }
    }
}
ADP
  • 123
  • 1
  • 2
  • 11

4 Answers4

7

You'll need Html.TextBoxFor instead of 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>
}

The difference between the two is explained here

Community
  • 1
  • 1
Bruno V
  • 1,721
  • 1
  • 14
  • 32
1

Model doesn't exist in the linq expression which is the parameter of @Html.TextBox(...). The m represents the Model and you need to use that variable to access the correct properties, like here:

<p>Customer Name: @Html.TextBoxFor(m => m.CustomerName)</p>
<p>ID:@Html.TextBoxFor(m=>m.CustomerId)</p>
peter
  • 14,348
  • 9
  • 62
  • 96
0

Try like this,

@model DataEntryMvcApplication.Models.Customer
@using (Html.BeginForm())
{
    <p>Customer Name: @Html.TextBox(m => m.CustomerName)</p>
    <p>ID:@Html.TextBox(m=>m.CustomerId)</p>
    <input type="submit" name="Custtomer" />
}
Jaimin
  • 7,964
  • 2
  • 25
  • 32
0

Just spent ages trying to solve this. After restoring old pages and making changes one by one, it appears the line causing the problem is:

<img src="~/images/Captcha/@ViewBag("CaptchaName")" />

I think it must not like attempts to access the view bag? Whatever, commenting this out solved the problem.

Andy Brown
  • 5,309
  • 4
  • 34
  • 39