我使用ASP.Net MVC 3,引用了所有正確的DLL(請參閱附帶的屏幕截圖),但出於某種原因,我收到了一個編譯錯誤。方法'Label'沒有重載需要3個參數
回答
這是一個奇怪的發生。我能夠重現你的錯誤。我還能夠弄清楚它爲什麼會發生,以及如何繞過它。
它爲什麼發生
有System.Web.Mvc.Html
和System.Web.WebPages.Html
什麼碰撞的結果是
System.Web.Mvc.Html
之間的命名空間衝突該命名空間包含一個定義該方法
LabelExtensions.Label (HtmlHelper, String, String)
正如你會發現,這是需要的先例,導致問題的命名空間。它只有兩個重載。 (String)
或(String, String)
。在測試時,這是唯一出現的選項。
如何繞過它
你真正想要的是這個命名空間
System.Web.WebPages.Html
這個命名空間適用於該方法
的定義HtmlHelper.Label (String, String, IDictionary<String, Object>)
您現在可以看到存在添加new { @class = "myClass" }
的可用性。
創建您自己的實例
獲取自己的HtmlHelper
,並繞過LabelExtension
這樣的:
剃刀
@{
var h = new System.Web.WebPages.Html.HtmlHelper();
}
ASP
<% var h = new System.Web.WebPages.Html.HtmlHelper(); %>
確保它沒有嵌套在剃刀或<%=
塊內,因爲它會阻止它可用。它需要位於頁面的全局級別。
使用正確的標籤
現在你可以使用這個:
剃刀
@h.Label("firstName", "First Name", new { @class = "control-label" })
ASP
<%= h.Label("firstName", "First Name", new { @class = "control-label" }) %>
而不是使用
<%=Html.Label("firstname","First Name",new {@class="control-label"})%>
嘗試使用下面的:
剃刀:
@Html.Label("firstname", "First Name" ,new { @class = "control-label" });
但是,我個人建議你不要這樣做。如果你有一個模型,並正在從它取名字,那麼你可以使用如下:
@Html.Label(model=>model.FirstName, "First Name" ,new { @class = "control-label" });
在ASPX視圖引擎:
<%=Html.Label(model=>model.FirstName,"First Name",new {@class="control-label"})%>
一定要添加當前的模型正如下圖頁面:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Project.Models.Whatever>" %>
我沒有使用Rzor,'@ '代碼不適用於Aspx渲染引擎。 –
該頁面已經有一個正在使用的模型,因此我不能使用另一個模型。除此之外,我的問題不是如何綁定模型(並且正確的方法是'LabelFor')。這是如何解決我的錯誤。 –
@JeremyBoyd:你看不到給定截圖中方法的參數嗎?顯然,他們說要添加一個像我所做的表達式... –
看着你~/Views/Web.config
看看有沒有configuration/system.web/pages/namespaces
節點
嘗試在那裏添加<add namespace="System.Web.Mvc.Html"/>
。
Label方法位於上面的命名空間中。看起來你的觀點沒有找到它。
由於您不編譯視圖,它的構建就好了。此外,你的智能感知從參考文獻中獲得了很好的方法。但是,這幾乎就像您的視圖沒有在運行時獲得方法。
將上述命名空間添加到web.config中可能會解決該問題,它會在您的應用程序中隱藏一個潛在的更大問題。 「修復」應該爲您進一步調試情況提供一個起點。
如果這樣可以解決您的問題,請嘗試將您的web.config文件與完全新的未更改的MVC項目進行比較。看看你有什麼不同,這可能會阻止視圖找到Html Helper。
它在Views/Web.Config其他'Html.Label()'方法工作,只是不是這個。 –
而其他Html.Label()實例具有相同的簽名? –
不,這是唯一的3參數簽名,另一個是2 - 沒有'object htmlAttributes' –
它看起來像你對我已經從Asp.Net的mvc 4挑過載,然後編譯器會抱怨,因爲它不Asp.Net MVC 3存在。
查看這些鏈接可查看不同版本的Asp中的Html.Label(...)
重載。淨MVC框架:
Asp.Net Mvc 2
Asp.Net Mvc 3
Asp.Net Mvc 4
基於對所鏈接網頁的信息,好像你的Visual Studio(出於某種原因)是給你的智能感知基於Asp.Net 4的mvc,但是當頁面被編譯時,Mvc版本3被使用,並且你選擇的重載不存在。
不知道爲什麼VS從4版雖然給智能感知...
看起來是正確的,但我沒有安裝MVC 4。我使用VS2010和.net 3.5 –
你,因爲你試圖使用HtmlExtension
不中ASP.NET MVC3存在默認情況下得到這個例外。您需要將HTML屬性支持添加到Html.Label()
。
基本上你需要添加你自己的擴展方法HtmlHelper
。您可以從MVC3源代碼開始。加入這個類:
public static class MyLabelExtensions
{
public static MvcHtmlString Label(this HtmlHelper html, string expression,
string labelText, object htmlAttributes)
{
return Label(html, expression, labelText,
new RouteValueDictionary(htmlAttributes));
}
public static MvcHtmlString Label(this HtmlHelper html, string expression,
string labelText, IDictionary<string, object> htmlAttributes)
{
return LabelHelper(html,
ModelMetadata.FromStringExpression(expression, html.ViewData),
expression, labelText, htmlAttributes);
}
// added htmlAttributes to the default MVC implementation
internal static MvcHtmlString LabelHelper(HtmlHelper html,
ModelMetadata metadata, string htmlFieldName,
string labelText = null,
IDictionary<string, object> htmlAttributes = null)
{
string str = labelText;
if (str == null)
{
string displayName = metadata.DisplayName;
if (displayName == null)
{
string propertyName = metadata.PropertyName;
if (propertyName == null)
str = Enumerable.Last(
htmlFieldName.Split(
new[]
{
'.'
}));
else
str = propertyName;
}
else
str = displayName;
}
string innerText = str;
if (string.IsNullOrEmpty(innerText))
return MvcHtmlString.Empty;
TagBuilder tagBuilder = new TagBuilder("label");
tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(
html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(
htmlFieldName)));
// this differs from MVC3 source code:
tagBuilder.MergeAttributes(htmlAttributes);
tagBuilder.SetInnerText(innerText);
// this is different too:
return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
}
}
添加完您可以按照您的觀點是這樣使用它們,這些擴展方法後:
<%= Html.Label("firstName", "First Name", new { @class = "control-label" }) %>
- 1. 方法'GetTouchPoint'沒有重載需要0個參數
- 2. 沒有重載方法「sqlQuery」需要0個參數 - C#錯誤
- 3. 沒有重載方法「GetKennel」需要1個參數C#
- 4. 「MVC3中沒有重載方法'LabelFor'需要2個參數」
- 5. 沒有重載方法需要兩個參數誤差在C#
- 6. 方法'LogException'沒有重載需要2個參數
- 7. 沒有重載方法需要兩個參數
- 8. 爲什麼我得到'沒有重載方法.createBanner需要'3'參數?
- 9. 沒有方法「GetValue」的過載需要1個參數
- 10. Monitor.Enter:沒有超載的方法需要2個參數
- 11. 方法'Given'沒有超載需要4個參數 - specflow
- 12. 方法'GetActiveInstructors'沒有超載需要2個參數
- 13. 沒有超載的方法''需要1個參數
- 14. 沒有超載的方法'距離'需要1個參數
- 15. 方法'Write'沒有超載需要2個參數
- 16. 沒有過載的方法「ToString」需要1個參數
- 17. 方法'tostring'沒有超載需要1個參數
- 18. 沒有超載的方法需要2個參數
- 19. C#沒有超載方法'ToString'需要1個參數 - 簡單
- 20. 如何解決「方法沒有超載」需要0個參數「?
- 21. 方法沒有超載,需要0個參數?
- 22. 方法'寫'沒有超載需要0個參數
- 23. 剃刀:沒有過載的方法'寫'需要0個參數
- 24. 沒有過載的方法需要4個參數
- 25. 沒有超載的方法需要0個參數
- 26. 方法'Write'沒有超載需要0個參數,MVC3 Razor
- 27. neo4jclient:CS1501:方法'OnCreate'沒有超載需要1個參數
- 28. 方法'DisplayOutput'沒有超載需要0個參數
- 29. 在EditorFor中顯示[Date]值:'ToString'沒有重載方法需要1個參數?
- 30. 實例化一個類 - 沒有重載方法'主題'需要'0'參數
這可能聽起來很基本,但你有沒有嘗試做一個乾淨的,然後重建?你是否也會在一個全新的項目中遇到同樣的錯誤? – keshav
@keshav,我總是乾淨和重建。是的,它確實發生在一個全新的ASP.Net MVC 3項目中。 –
對我來說,似乎你的安裝有問題。你可以嘗試重新安裝... – keshav