3

I'm trying to create a helper that will make my form controls in the whole website

Here is what i use till now

@helper editorField(System.Linq.Expressions.Expression<Func<Model, String>> o)
{
    <div class="form-group">
        @Html.LabelFor(o, new { @class = "col-md-4 control-label" })
        @Html.ValidationMessageFor(o, "", new { @class = "col-md-6 col-xs-12  text-errer" })
        <div class="col-md-5">
            @Html.TextBoxFor(o, new { @class = "form-control" })
        </div>
    </div>
}

And the way i use it is

        @editorField(model => model.Name)
        @editorField(model => model.Email)
        @editorField(model => model.PhoneNumber)

this make it really easy to change the style and layout of the whole website in 1 place

Now here is the problem

I need to make helper for each Model and data type

Im looking for a way to make it work something like this

@helper editorField(object o)
{
        <div class="form-group">
            @Html.LabelFor(o, new { @class = "col-md-4 control-label" })
            @Html.ValidationMessageFor(o, "", new { @class = "col-md-6 col-xs-12  text-errer" })
            <div class="col-md-5">
                @Html.TextBoxFor(o, new { @class = "form-control" })
            </div>
        </div>
}

and it should work for all Models and datatypes

CMS
  • 3,657
  • 1
  • 27
  • 46
  • 2
    Have a look at this: http://stackoverflow.com/questions/15968305/html-labelfor-use-displayname-of-object-not-property – IndieTech Solutions Apr 02 '15 at 18:07
  • 2
    Here are my thoughts on this question: Something like this would be best defined as an Html helper extension (or set of extensions) in a separate code file. You would have to implement this for each type you want to allow, or you would have to take an object, figure out its type, and cast it. I would just implement it for each type. What more do you need than `int` and `string`? – Nick Apr 02 '15 at 18:45
  • i need int, string, decimal , and ,double – CMS Apr 02 '15 at 18:52
  • 1
    [Refer this example](http://stackoverflow.com/questions/26955073/converting-asp-net-mvc-razor-helper-function-into-a-method-of-a-helper-class/26955246#26955246) for creating a custom helper –  Apr 02 '15 at 23:15

1 Answers1

0

So basically what you are trying to achieve is a generic HTML helper. Unfortunately those are not supported directly in Razor views. But, you can use this workaround:

@functions
{
    public HelperResult PasteEditorFor<TModel, TItem>(HtmlHelper<TModel> html, Expression<Func<TModel, TItem>> expr)
    {
        return editorField(
            html.LabelFor(expr, new { @class = "col-md-4 control-label" }), 
            html.ValidationMessageFor(expr, "", new { @class = "col-md-6 col-xs-12  text-errer" }), 
            html.TextBoxFor(expr, new { @class = "form-control" }));
    }
}

@helper editorField(MvcHtmlString label, MvcHtmlString validationMessage, MvcHtmlString textBox)
{
     <div class="form-group">
         @label
         @validationMessage
         <div class="col-md-5">
            @textBox
         </div>
     </div>
 }

// usage sample
@PasteEditorFor(Html, m => m.LongDescription)

But as it is still not easy reusable (can't tell at the moment how to make it visible though all views), so I would recommend you to write usual HtmlHelper with usual static class syntax ( look at Alundra the dreamwalker comment) and add it's namespace to list of default one in WebConfig.

Community
  • 1
  • 1
Yura
  • 2,013
  • 19
  • 25
  • to make it visible to all views i put it in a app_code folder in a myHelpers.cshtml file and then i call it myHelpers.editorField(...) – CMS Apr 02 '15 at 18:31
  • how do i call the helper do i need t pass in a html helper as a parameter? – CMS Apr 02 '15 at 18:44
  • ow yeah i missed it , the issue is that i really don't want to use @functions in my code since i feel it should be in a .cs file and that what im going to do, and as im typing it i see that i get already 2 comments about it stating the same so will go for it. thanks alot – CMS Apr 02 '15 at 18:51