Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HtmlHelper扩展更改传入的属性的值_C#_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

C# HtmlHelper扩展更改传入的属性的值

C# HtmlHelper扩展更改传入的属性的值,c#,asp.net-mvc,asp.net-mvc-3,C#,Asp.net Mvc,Asp.net Mvc 3,我有一个html助手以友好的方式显示文本框的电话号码。用法: Html.PhoneNumberFor(m => m.PhoneNumber) 我希望它取一个像“1111111111”这样的数字,然后输出“(111)111-1111”。我曾尝试通过从html helper扩展方法中的表达式中获取属性来更新html helper的viewdata,但这似乎不起作用。那么,有人知道如何更新表达式对象中属性的值吗?以下是不起作用的代码: public static MvcHtmlString P

我有一个html助手以友好的方式显示文本框的电话号码。用法:

Html.PhoneNumberFor(m => m.PhoneNumber)
我希望它取一个像“1111111111”这样的数字,然后输出“(111)111-1111”。我曾尝试通过从html helper扩展方法中的表达式中获取属性来更新html helper的viewdata,但这似乎不起作用。那么,有人知道如何更新表达式对象中属性的值吗?以下是不起作用的代码:

public static MvcHtmlString PhoneNumberFor<TModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, string>> expression, object htmlAttributes)
    {
        var value = ModelMetadata.FromLambdaExpression(expression, helper.ViewData).Model as string;

        if (!string.IsNullOrEmpty(value) && value.Length == 10)
        {
            value = string.Format("({0}){1}-{2}", value.Substring(0, 3), value.Substring(3, 3), value.Substring(6));                

            var fieldName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
            helper.ViewData[fieldName] = value;

        }

        return helper.TextBoxFor(expression, htmlAttributes);           
    }
public static MvcHtmlString PhoneNumberFor(此HtmlHelper帮助程序、表达式、对象htmlAttributes)
{
var value=modelmetada.FromLambdaExpression(表达式,helper.ViewData)。模型为字符串;
如果(!string.IsNullOrEmpty(value)&&value.Length==10)
{
value=string.Format(({0}){1}-{2}),value.Substring(0,3),value.Substring(3,3),value.Substring(6));
var fieldName=helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(表达式));
helper.ViewData[fieldName]=值;
}
返回helper.TextBoxFor(表达式,htmlAttributes);
}

我将为此创建一个自定义DisplayFormatAttribute,然后您只需将其应用于PhoneNumber属性

这对你有用吗?如果没有,请解释一下你需要什么才能让它发挥作用

如果您希望继续使用字符串值作为输入,那么这应该是可行的

var formattedPhone = 
    String.Format("{0:(###)###-####}", Convert.ToInt64("1111111111"));

在有
value的地方更改代码。例如,子字符串(0,3)
,并生成一个名为
areacode
的局部变量,然后对其他2个子字符串值执行相同操作,并将其作为参数传递给字符串。格式化方法单步执行时,“value”变量具有正确的值。你的可能更具可读性,但我不确定它将如何解决我的问题..你能不能像这样做
int-valuePhone=1111;var valuePhoneFrmt=string.Format(“{0:(#####)###-#####,valuePhone”)如我所说,这仍然不能解决文本框中的值没有用我在这里的视图数据字典helper.ViewData[fieldName]=value;中设置的值进行更新的问题;。我还需要转换成一个不需要的整数。谢谢你的评论,但我实际上还需要在这个助手中做一些其他的事情。而且,我必须去改变各种各样的视图模型,这比改变视图要耗费更多的时间。helper有一些问题。ViewData[fieldName]=formattedStr。它实际上并没有像我需要的那样更新模型。这是因为需要格式化的值需要是
Int32
值吗?我相信
string.Format需要返回一个字符串,但它的格式化值需要是一个
Int`我尝试了我在上面的注释部分中作为字符串保留的代码,但它不会格式,但我想知道是否需要
IFormatProvider
。无论哪种方式。。注释后的代码可以工作,但我仍然不知道问题是什么,您是否可以这样做以使其工作
String.Format(“{0:(#####)############,”,Convert.ToInt64(“1111111111111”)它与字符串格式无关。那很好。事实上,我正在更新ViewData字典,但它似乎并没有实际使用新值进行更新。