0

I've added a class for the HTML Custom Extensions:

using System;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace App.MvcHtmlHelpers    
{
public static class HtmlHelperExtensions
{

    public static MvcHtmlString ComboBox(HtmlHelper html, string name, SelectList items, string selectedValue)
    {
        var sb = new StringBuilder();
        sb.Append(html.DropDownList(name + "_hidden", items, new { @style = "width: 200px;", @onchange = "$('input#" + name + "').val($(this).val());" }));
        sb.Append(html.TextBox(name, selectedValue, new { @style = "margin-left: -199px; width: 179px; height: 1.2em; border: 0;" }));
        return MvcHtmlString.Create(sb.ToString());
    }

    public static MvcHtmlString ComboBoxFor<TModel, TProperty>(HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, SelectList items)
    {
        var me = (MemberExpression)expression.Body;
        var name = me.Member.Name;

        var sb = new StringBuilder();
        sb.Append(html.DropDownList(name + "_hidden", items, new { @style = "width: 200px;", @onchange = "$('input#" + name + "').val($(this).val());" }));
        sb.Append(html.TextBoxFor(expression, new { @style = "margin-left: -199px; width: 179px; height: 1.2em; border: 0;" }));
        return MvcHtmlString.Create(sb.ToString());
    }

I've also registered it in my site web config:

<namespaces>
<add namespace="System.Web.Helpers" /> 
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />        
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
<add namespace="App.MvcHtmlHelpers"/>
</namespaces>

In my view, I import the namespace:

<%@ Import Namespace="RSPWebApp.MvcHtmlHelpers" %>

But when I go to call it in the view, it doesn't recognize the custom extension. Can someone help me by telling me what I might have missed? Thanks so much in advance! <%:Html.ComboBoxFor(a => a.Street2, streetAddressListItems) %>

3 Answers 3

2

You might be missing this keyword:

public static MvcHtmlString ComboBox(this HtmlHelper html, string name, SelectList items, string selectedValue)

Also if check your Web.config in your Views directory for something similar to this:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <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.Routing" />
        <add namespace="RSPWebApp.MvcHtmlHelper" />
      </namespaces>
    </pages>
</system.web.webPages.razor>

It seems that instead of the project Web.config, you have to explicitly add the namespace to the Razor engine itself in the Views folder.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think we can tell if OP is using Razor views or not.
@JofryHS, is correct, you are missing the "this" keyword and are putting the reference in the wrong Web.Config
0

You're missing the this keyword in your extension methods. Try this:

public static MvcHtmlString ComboBox(this HtmlHelper html, string name, SelectList items, string selectedValue)
    {
        var sb = new StringBuilder();
        sb.Append(html.DropDownList(name + "_hidden", items, new { @style = "width: 200px;", @onchange = "$('input#" + name + "').val($(this).val());" }));
        sb.Append(html.TextBox(name, selectedValue, new { @style = "margin-left: -199px; width: 179px; height: 1.2em; border: 0;" }));
        return MvcHtmlString.Create(sb.ToString());
    }

Also the Import Namespace you provided doesn't appear to be the same namespace as your extension methods. However adding them to the web.config should have you covered anyway.

Comments

0

Thanks everyone for all your help. I was indeed missing the "this" keyword in my HTMLHelper class (it originally came up red in my resharper, so I shouldn't trust that all the time!)

And I added it to my web.config in the Views folder (although oddly it's grayed out) . To answer a previous comment I am actually using ASP.Net views as I'm new to MVC and didn't want to wrestle with Razor+MVC.

It works perfectly now, thank you!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.