【发布时间】:2016-07-08 21:40:59
【问题描述】:
我有一个“PhoneNumber”字符串,我需要将其解析为 3 个部分。我需要将每个部分显示在视图中的单独字段中。我知道如何通过向模型中添加其他字符串来实现这一点,但问题是我不再能做到这一点。我需要能够使用组捕获部件并以某种方式通过“PhoneNumber”访问它们。这可能吗?
型号:
public string PhoneNumber
{
get
{
return _phoneNumber;
}
set
{
Regex regexObj = new Regex(@"[^\d]");
_phoneNumber = regexObj.Replace(value, "");
var match = Regex.Match(_phoneNumber, @"(\d{3})(\d{3})(\d{4})");
if (match.Success)
{
_phoneNumber = string.Format("({0}) {1}-{2}", match.Groups[1], match.Groups[2], match.Groups[3]);
}
}
}
private string _phoneNumber;
查看(这是我需要分隔组的地方):
@Html.TextBoxFor(model => model.Pharmacy.PhoneNumber, new { @id = "txtPhoneNumber", @Name = "txtPhoneNumber" })
【问题讨论】:
-
上面的代码有什么问题。你期待什么?
-
@Venky 显示所有 10 位数字。我需要为视图中的前 3 位数字、接下来的 3 位数字和最后 4 位数字设置一个单独的文本框。
-
请注意,model-view-controller 标签是针对有关模式的问题。 ASP.NET-MVC 实现有一个特定的标记。
标签: c# regex asp.net-mvc