我正在尝试验证ip地址使用MVC 3。如果用户输入错误的ip地址,那么页面应该显示“错误的ip地址信息”给用户。我在网上搜索了很多次,但没有得到适当的解决方案。你能帮我..。提前谢谢。
Model的代码:
[Display(Name = "EQ")]
public string EQ { get; set; }
[Display(Name = "EQD")]
public string EQD { get; set; }
[Display(Name = "BFX")]
public string BFX { get; set; }
[Display(Name = "SLB")]
public string SLB { get; set; }
[Display(Name = "Others")]
public string Others { get; set; }我想分开5个复选框,即(EQ,EQD,BFX,SLB和其他)。
视图代码:
<div class="editor-label" style="font-weight: bold">
@Html.LabelFor(model => model.EQ)
</div>
<div class="editor-field">
@Html.CheckBoxFor(Model => Model.EQ)
@Html.ValidationMessageFor(model => model.EQ)
</div>
<div class="editor-label" style="font-weight: bold">
@Html.LabelFor(model => model.EQD)
</div>
<div class="editor-field">
@Html.CheckBoxFor(Model => Model.EQD)
@Html.ValidationMessageFor(model => model.EQD)
</div>
<div class="editor-label" style="font-weight: bold">
@Html.LabelFor(model => model.BFX)
</div>
<div class="editor-field">
@Html.CheckBoxFor(Model => Model.BFX)
@Html.ValidationMessageFor(model => model.BFX)
</div>
<div class="editor-label" style="font-weight: bold">
@Html.LabelFor(model => model.SLB)
</div>
<div class="editor-field">
@Html.CheckBoxFor(Model => Model.SLB)
@Html.ValidationMessageFor(model => model.SLB)
</div>
<div class="editor-label" style="font-weight: bold">
@Html.LabelFor(model => model.Others)
</div>
<div class="editor-field">
@*@Html.EditorFor(model => model.Others)*@
@Html.CheckBoxFor(Model => Model.Others)
@Html.ValidationMessageFor(model => model.Others)
</div>发布于 2014-10-16 15:15:06
我推荐使用IPAddress.TryParse()
确定字符串是否为有效的IP地址。
http://msdn.microsoft.com/en-us/library/system.net.ipaddress.tryparse(v=vs.110).aspx
发布于 2017-04-12 05:20:16
在模型中
[RegularExpression(@"^(?:0-9{1,3}.){3}0-9{1,3}$")]
公共字符串AssignedIP { get;set;}
发布于 2019-01-05 02:53:57
这家伙对MVC https://codingjourneyman.com/2015/01/08/model-validation-in-asp-net-mvc/有一个很好的方法。
public class IPAddressModel
{
[Required]
[Display(Name = "Your name")]
public string Name { get; set; }
[IpAddress]
[Required]
[Display(Name = "Your IP address")]
public string IPAddress { get; set; }
}https://stackoverflow.com/questions/26398027
复制相似问题