2014-03-19 123 views
1

創建一個只讀的文本框,我使用如何在ASP.NET MVC4剃刀

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

,但得到一個錯誤。什麼是正確的語法?

錯誤:

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

指定類型。 'TextBoxFor '(假設存在) – Will

+1

我認爲問題出在你的字符串文字「代碼」而不是「只讀」。這需要一個與你的模型相關的lambda。例如,如果您有一個名爲'code'的屬性,那麼執行此類錯誤時出現錯誤消息CS1963:表達式樹可能包含一個名爲'code'的屬性,@ Html.TextBoxFor(m => m.code,new {@ readonly =「readonly」})' – Tom

+0

不包含動態操作 – Nisar

回答

2

假設你有你的觀點的模型,你可以使用這樣的事情,其中​​「模型」代表你的模型:

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

你應該嘗試<td>@Html.TextBox("code",new { @readonly="readonly" })</td>,當使用TextBoxFor您需要將ViewModel中的屬性與lambda表達式一起傳遞。

1

對於@Html.TextBoxFor()使用:

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

對於@Html.TextBox()使用:

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

,謝謝它正在工作 – Binay