[RESOLVED]Custom HTML Helper method.

Hi ,

i have created my custom Textbox with validation. but i am not able to see error messages in UI when i click on submit button.Everything is working fine.

only thing i can’t see error messgae.

my method

public
static
MvcHtmlString
CustomTextBoxcontrol(
this
HtmlHelper
helper,
string
name,
string
text,
int
maxlength,
bool
isManatory,
object
htmlAttributes)

        {

TagBuilder
textBox =
new
TagBuilder("input");

   textBox.Attributes.Add("type",
"text");

   textBox.Attributes.Add("name",
name);

    textBox.Attributes.Add("id",
"txt"
+ name);

    if
(isManatory ==
true)

            {

                textBox.Attributes.Add("aria-required",
"true");

                textBox.Attributes.Add("data-val",
"true");

                textBox.Attributes.Add("data-val-required",
"The
"
+ name +
"
field is required."
);

               

            }

                  textBox.MergeAttributes(new
RouteValueDictionary(htmlAttributes));

           return
MvcHtmlString.Create(textBox.ToString(TagRenderMode.Normal));

        }

In UI -Rendered code-(F12 code)

<input name="Student 0" class="input-validation-error" id="txtStudent 0" aria-required="true" style="color: red;" type="text" maxLength="3" data-val-required="The Student 0 field is required." data-val="true"/>

i have added the below ref

<script src="/Scripts/jquery.validate.min.js"></script>

    <script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>

    <script src="/Scripts/jquery.unobtrusive-ajax.min.js"></script>

if you have any sampe with validation,please post for me

Please help me on this.

Thanks,

Sundar.

Hi Sundar,

Base on your code, I made a test and it works fine.

This is my code:

<div>
            @Html.CustomTextBoxcontrol("Student 0","gg",3,true,new{@class="tt"})
            @Html.ValidationMessage("Student 0")
        </div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

First, please check whether the jQuery file is loaded. (Using developer tool to check it)

Secondly, please load the jquery.unobtrusive-ajax.min.js file first (before validate.min.js and validate.unobtrusive.min.js).

Thirdly, please create a new project and try again. If it still have the issue, please share the project on the OneDrive.

Best Regards

Starain

Hi Starain,

Thanks you so much for your reply.

Its working fine what i expected.

 

Thanks,

Sundar.

Leave a Reply