2013-09-21 37 views
5

我一直在瀏覽網頁,試圖找到一個很好的示例/教程,詳細說明如何創建和使用我自己的自定義HTML助手爲我的MVC 3 Razor應用程序,我發現這是一個如下向MVC項目添加自定義HTML助手

Adding your own HtmlHelper in ASP.NET MVC 3

我創建了一個類(修剪下來一點)爲使

using System; 
using System.Collections.Generic; 
using System.Globalization; 
using System.Linq; 
using System.Linq.Expressions; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Mvc.Html; 

namespace MyWebApp 
{ 
    public static class ExtensionMethods 
    { 
     public static MvcHtmlString StateDropDownListFor<TModel, TValue> 
         (this HtmlHelper<TModel> html, 
             Expression<Func<TModel, TValue>> expression) 
     { 
      Dictionary<string, string> stateList = new Dictionary<string, string>() 
      { 
       {"AL"," Alabama"}, 
       {"AK"," Alaska"}, 
       {"AZ"," Arizona"}, 
       {"AR"," Arkansas"} 

       }; 
       return html.DropDownListFor(expression, 
         new SelectList(stateList, "key", "value")); 
     } 

    } 
} 

到目前爲止好,

在我的控制器我還添加了參考現在

using System.Web.Mvc.Html; 

我的觀點裏,我有以下

@Html.StateDropDownList(x => x.State) 

但我得到以下錯誤

System.web.mvc.htmlhelper<system.collections.generic.list<Profile.ProfileImages>> does  not contain a definition for StateDropDownList and no extension method  StateDropDownList acception a first argument of type  system.web.mvc.htmlhelper<System.Collections.Generic.List<Profile.ProfileImages>> could be  found(Are you missing a using directive of an assembly reference?) 

可能有人請告訴我是什麼即時在這裏做錯了。

+0

您是否有一個名爲DisplayImageFor的HTML幫助器? –

+0

@UfukHacıoğulları對不起,我添加了不正確的錯誤陳述,我更新了上述內容。 –

回答

15

你應該在你的視圖中的命名空間:

@using MyWebApp 

或者你可以導入這個命名空間爲所有從web.config文件的意見。

<system.web.webPages.razor> 
    <pages pageBaseType="System.Web.Mvc.WebViewPage"> 
    <namespaces> 
     <add namespace="System.Web.Mvc" /> 
     <add namespace="System.Web.Mvc.Ajax" /> 
     <add namespace="System.Web.Mvc.Html" /> 
     <add namespace="System.Web.Optimization" /> 
     <add namespace="System.Web.Routing" /> 
     <add namespace="MyWebApp" /> 
    </namespaces> 
    </pages> 
</system.web.webPages.razor> 
+0

謝謝你的幫助。 –

3

我認爲一個更容易的辦法是做到以下幾點:1。 創建一個視圖像往常一樣,把你的助手在像往常一樣的代碼和HTML的搭配,只要你喜歡。 2.將視圖移至App_Code文件夾。 3.獲取你所有的意見有助於爲左右(注意_MyHelpers是在App_Code文件夾的視圖的名稱):

@_MyHelpers.JQMRadioTrueFalse("Voice Mail on your Home Phone?", "HomePhoneHasAnswerPhone", Model.TrueFalse, t.HomePhoneHasAnswerPhone) 

這將是這是App_Code文件夾視圖的例子如上所示訪問任何視圖:

@helper JQMRadioList(string Legend, string RadioListName, List<Fizz.DomainClasses.GenericClasses.GenericDropDownOption> options, bool Horizontal = false, string CurrentSelection = "") 
    { 
     <fieldset data-role="controlgroup" data-mini="true" data-theme="b" @((Horizontal) ? " data-type=\"horizontal\"" : "")> 
      <legend>@Legend:</legend> 
      @foreach (var li in options) 
      { 
       @JQMRadioListItem(RadioListName, li.TheValue, li.Text, CurrentSelection) 
      } 
     </fieldset> 
    } 
+0

不知道誰投下了這個,但我可以問問是什麼原因? – Michael

+0

此答案顯示如何在視圖中創建助手。它是基於代碼的視圖的替代方法,沒有理由爲downvote。 –

相關問題