在图像工具提示中添加ValidationMessageFor

时间:2018-06-25 07:33:37

标签: c# asp.net asp.net-mvc validation tooltip

我想在图像的标题(工具提示)中显示ValidationMessageFor

现在,确认消息显示在输入框下方,但是错误消息可能会很长,因此我想将其纳入工具提示中。

    public class CreateStep
{
    [RegularExpression("^[a-zA-Z]*$", ErrorMessage = "Only Alphabets are Allowed")]
    [Required(ErrorMessage = "Please Enter Username")]
    public string Name { get; set; }

    [RegularExpression("^[1-9]*$", ErrorMessage = "Only numbers are allowed")]
    [Required(ErrorMessage = "Please Enter Email")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Please Enter Address")]
    public string Address { get; set; }

}

“我的视图”代码看起来像这样

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div style="float:left; height:100vh; width:50%; border-style: solid; border-color:lightskyblue; border-right: 0px">
        DASHBOARD
    </div>

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div style="float:right; height:100vh; width:50%; border-style: solid; border-color:lightskyblue">
        <input class="btn btn-primary btn-lg" type="button" style="margin:10px;" value="Create" onclick="showDiv()" />
        <div class="form-group" id="CreateDiv" style="display:none; margin:10px;">

            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Name, new { @class = "form-control", @placeholder = "Enter your name" })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>

            <br />

            <div class="editor-label">
                @Html.LabelFor(model => model.Email)
            </div>
            <div class="editor-field">

                @Html.TextBoxFor(model => model.Email, new { @class = "form-control", @placeholder = "Enter your email" })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) 


                <img src="~/Images/QuestionMark.png" width="15" height="15" title="@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) "/>

            </div>


            <br />

            <div class="editor-label">
                @Html.LabelFor(model => model.Address)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Address, new { @class = "form-control", @placeholder = "Enter your address" })
                @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
            </div>

            <br />

            <input class="btn btn-primary btn-lg" type="button" value="Submit" />


        </div>

    </div>
}

在查看代码中,我尝试包括ValidationMessageFor,但它仅显示<span = ...而不是错误消息。

希望您有解决方案。

0 个答案:

没有答案