C# MVC扩展方法无法将数据返回到视图

C# MVC扩展方法无法将数据返回到视图,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我是MVC的新手,正在尝试我的第一个电话号码格式扩展方法。我以前认为我正在学习的所有逻辑都违背了MVC的原则。因此,为了代码重用和遵循MVC标准实践,我尝试使用这种扩展方法 namespace Data.CustomHtmlHelper { public static class CustomerHelperHelper { public static string PhoneNumber(this HtmlHelper helper, string value) {

我是MVC的新手,正在尝试我的第一个电话号码格式扩展方法。我以前认为我正在学习的所有逻辑都违背了MVC的原则。因此,为了代码重用和遵循MVC标准实践,我尝试使用这种扩展方法

namespace Data.CustomHtmlHelper
{
public static class CustomerHelperHelper
 {
    public static string PhoneNumber(this HtmlHelper helper, string value)
    {
        value = new System.Text.RegularExpressions.Regex(@"\D")
            .Replace(value, string.Empty);
        value = value.TrimStart('1');
        if (value.Length == 7)
            return Convert.ToInt64(value).ToString("###-####");
        if (value.Length == 10)
            return Convert.ToInt64(value).ToString("###-###-####");
        if (value.Length > 10)
            return Convert.ToInt64(value)
                .ToString("###-###-#### " + new String('#', (value.Length - 10)));
        return value;
    }
 }
}
这个例子给了我一个错误消息“该值不能为null”

试试第二个:

     public static string PhoneNumber(this HtmlHelper helper, string value)
    {

        if (!String.IsNullOrEmpty(value))
        {
            if (value != null && value.Length == 7)
                return Convert.ToInt64(value).ToString("###-####");
            if (value != null && value.Length == 10)
                return Convert.ToInt64(value).ToString("###-###-####");
            if (value != null && value.Length > 10)
                return Convert.ToInt64(value)
                    .ToString("###-###-#### " + new String('#', (value.Length - 10)));
        }

        else if (String.IsNullOrWhiteSpace(value))
        {
            return Convert.ToInt64(value).ToString("###-####");
        }
        return value;
    }
这将不会返回视图中电话号码的正确值,即“所有存在的内容均为“-”

试试第三个:

       public static string PhoneNumber(this HtmlHelper helper, string value)
    {
        if (value != null && value.Length > 6)
            return "Invalid Phone Number";
        if (value != null && value.Length == 7)
            return Convert.ToInt64(value).ToString("###-####");
        if (value != null && value.Length == 10)
            return Convert.ToInt64(value).ToString("###-###-####");
        if (value != null && value.Length > 10)
            return Convert.ToInt64(value)
                .ToString("###-###-#### " + new String('#', (value.Length - 10)));
        return value;
    }
这不会在视图中返回任何内容

对所有对象使用相同的视图

@using Data.CustomHtmlHelper

  <div class="col-md-4">                             
     @Html.PhoneNumber(Model.phone_no)
  </div>
@使用Data.CustomHtmlHelper
@Html.PhoneNumber(型号、电话号码)

只需在代码中放置一个断点,就可以找出扩展方法返回的原因。听起来您想使用自定义HTML帮助程序,我建议您使用
MvcHtmlString
例如
public static MvcHtmlString PhoneNumber
&返回HTML字符串(例如
返回MvcHtmlString.Create(value.ToString())在try#3中,如果(value!=null&&value.Length>6),则此行不正确。
->如果(value!=null&&value.Length),则应为
if(value!=null&&value.Length