Nisar
Nisar

Reputation: 6038

How to create a read-only textbox in ASP.NET MVC4 Razor

I am using

 <td>@Html.TextBoxFor("code",new { @readonly="readonly" })</td>

but getting an error. What is the right syntax?

The error:

Error   1   The type arguments for method 'System.Web.Mvc.Html.InputExtensions.TextBoxFor<TModel,TProperty>(System.Web.Mvc.HtmlHelper<TModel>, System.Linq.Expressions.Expression<System.Func<TModel,TProperty>>, System.Collections.Generic.IDictionary<string,object>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.  c:\Users\ATPL\Documents\Visual Studio 2012\Projects\AST_MGMT\AST_MGMT\Views\Department\Add.cshtml   7   18  AST_MGMT

Upvotes: 1

Views: 2339

Answers (3)

Jeyhun Rahimov
Jeyhun Rahimov

Reputation: 3787

For @Html.TextBoxFor() use:

@Html.TextBoxFor(model => model.code, new { @readonly="readonly" })

For @Html.TextBox() use:

@Html.TextBox("code", "default value ", new { @readonly="readonly" })

Upvotes: 1

Guillermo Oramas R.
Guillermo Oramas R.

Reputation: 1303

You should try <td>@Html.TextBox("code",new { @readonly="readonly" })</td>, when using TextBoxFor you need to pass a property from the ViewModel with a lambda expression.

Upvotes: 2

Big Daddy
Big Daddy

Reputation: 5224

Assuming you have a model in your view, you can use something like this where "model" represents your model:

@Html.TextBoxFor(model => model.code, new { @readonly="readonly" })

Upvotes: 3

Related Questions