none
What happened with older threads in the Visual C# language forum and how can I use this code? RRS feed

  • Question

  • Hi everybody,

    I found that Visual C# language forum has been archived and all its threads became locked. Do you know if this is a permanent situation or these threads are going to be moved to the only C# forum we now have? 

    Anyway, here is my question.

    I added just now this code

    using System.Collections.Generic;
    using System.Linq.Expressions;
    using WebDemo.Helper;
    
    namespace System.Web.Mvc.Html
    {
        public static class CustomHtmlExtensions
        {
            public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, EditorTemplate editorTemplate)
            {
                return html.EditorFor(expression, editorTemplate.ToString());
            }
    
            public static bool IsNumeric(object obj)
            {
                return (obj == null) ? false : IsNumeric(obj.GetType());
            }
    
            public static bool IsNumeric(Type type)
            {
                if (type == null)
                    return false;
    
                TypeCode typeCode = Type.GetTypeCode(type);
    
                switch (typeCode)
                {
                    case TypeCode.Byte:
                    case TypeCode.Decimal:
                    case TypeCode.Double:
                    case TypeCode.Int16:
                    case TypeCode.Int32:
                    case TypeCode.Int64:
                    case TypeCode.SByte:
                    case TypeCode.Single:
                    case TypeCode.UInt16:
                    case TypeCode.UInt32:
                    case TypeCode.UInt64:
                        return true;
                }
                return false;
            }
        }
    }

    I took the first part of this code from this blog http://dotnetspeak.com/index.php/2012/10/asp-net-mvc-template-and-knockout-js and the second part from this thread http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/66a7dc8d-f276-4d45-8da4-f8d9857db52c/ If that thread would have remained opened, I would vote in that thread and ask my question there. Since it's locked, I am asking a new question.

    In my razor view I have this right now

    <div class="editor-label">
        @Html.Label((ViewData.ModelMetadata.DisplayName??ViewData.ModelMetadata.PropertyName),
            new Dictionary<string, object>
                {
                    { "for", ViewData.ModelMetadata.PropertyName }
                })
    </div>
    @if (ViewData.ModelMetadata.ModelType.IsNumeric()
    <div class="editor-field">
        @Html.TextBox("", (object)Model,
            new Dictionary<string, object>
                {
                    { "id", ViewData.ModelMetadata.PropertyName },
                    { "name", ViewData.ModelMetadata.PropertyName },
                    { "class", "text-box single-line"},
                    { "data-bind", "value: " + ViewData.ModelMetadata.PropertyName },
                })
        @Html.ValidationMessage(ViewData.ModelMetadata.PropertyName,
            new Dictionary<string, object>
                {
                    { "data-valmsg-for", ViewData.ModelMetadata.PropertyName }
                })
    </div>
    

    Look at the if part. Here I want to add the check for the type being Numeric. Given the code above and the place I put it (did I put it correctly?), how can I invoke that static method? 

    Can you please show me the code?

    Thanks a lot in advance.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    Tuesday, December 25, 2012 12:10 AM
    Moderator

Answers

  • I am not an expert in Razor but I can see that your IsNumeric method is not extension method. So, you need to modify the signature as below

    public static bool IsNumeric(this Type type)
    {
       ...
    }
    
    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, December 25, 2012 9:00 AM

All replies