Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/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
Asp.net mvc 如何在asp mvc中设置cshtml razor文件中的正则表达式?_Asp.net Mvc_Razor_Custom Validators - Fatal编程技术网

Asp.net mvc 如何在asp mvc中设置cshtml razor文件中的正则表达式?

Asp.net mvc 如何在asp mvc中设置cshtml razor文件中的正则表达式?,asp.net-mvc,razor,custom-validators,Asp.net Mvc,Razor,Custom Validators,我想在我的cshtml文件中设置正则表达式,请帮我怎么做?我不想先在代码中使用正则表达式 <div class="form-group"> @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => m

我想在我的cshtml文件中设置正则表达式,请帮我怎么做?我不想先在代码中使用正则表达式

<div class="form-group">
    @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })

    <div class="col-md-10">
       @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control", placeholder = "short name", required = "required", RegularExpressionAttribute = @"^([a-zA-Z .&'-]+)$" } })
       @Html.ValidationMessageFor(model => model.Title,"", new { @class = "text-danger" })
    </div>
</div>

@LabelFor(model=>model.Title,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.EditorFor(model=>model.Title,new{htmlAttributes=new{@class=“form control”,placeholder=“short name”,required=“required”,RegularExpressionAttribute=@“^([a-zA-Z.&'-]+)$”})
@Html.ValidationMessageFor(model=>model.Title,“,new{@class=“text danger”})

这很有效。您需要将EditorFor更改为TextBoxFor。删除htmlAttributes,并使用pattern而不是RegularExpressionAttribute:

型号:

namespace Testy20161006.Models
{
    public class Student
    {
        public int ID { get; set; }
        public String Name { get; set; }
        public String email { get; set; }
    }

}
控制器:

    [HttpPost]
    public ActionResult Index9(Student stud)
    {
        if (ModelState.IsValid)
        {
            var ap = "dh";
        }
        return View(stud);
    }

    public ActionResult Index9()
    {
        return View(new Student());
    }
视图:

@model testy2016.1006.Models.Student
@{
布局=空;
}
指数9
@使用(Html.BeginForm())
{
@LabelFor(model=>model.Name,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.TextBoxFor(model=>model.Name,新的{@class=“form control”,placeholder=“short Name”,required=“required”,pattern=@“^([a-zA-Z.&'-]+)$”)
@Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”})
}

以及如何对不使用的模式使用验证消息:@Html.ValidationMessageFor(model=>model.Name,Resources.Milsa.Milsa_RequiredField,new{@class=“text danger”})
@model Testy20161006.Models.Student

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index9</title>
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <link href="~/Content/Student.css" rel="stylesheet" />
</head>
<body>
    <div>
        @using (Html.BeginForm())
        {
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            @Html.TextBoxFor(model => model.Name, new { @class = "form-control", placeholder = "short name", required = "required", pattern = @"^([a-zA-Z .&'-]+)$"  })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            <input type="submit" value="Submit" />
        }
    </div>
</body>
</html>