GeekyNuns
GeekyNuns

Reputation: 298

Validation is not working mvc

I just can`t find out what is wrong. I am trying no to allow setting null values to field, when user tries to do it, it must show message "*", and wait for him to set values, but it doesnot work and null values are successfully sent to an action. Model :

public class CompanyMainInfoModel
{
    public int CompanyId { get; set; }

    [Required(ErrorMessage = "*")]
    [Display(Name = "company_name", ResourceType = typeof(Localization))]
    public string CompanyName { get; set; }

    [Required(ErrorMessage = "*")]
    [Display(Name = "company_address", ResourceType = typeof(Localization))]
    public string CompanyAddress { get; set; }

    [Required(ErrorMessage = "*")]
    [Display(Name = "company_director", ResourceType = typeof(Localization))]
    public string CompanyDirector { get; set; }

    [Required(ErrorMessage = "*")]
    [Display(Name = "company_phone", ResourceType = typeof(Localization))]
    public string CompanyTelephoneNumber { get; set; }
}

Markup :

    @model BTGHRM.Models.CompanyMainInfoModel
@{
    Layout = "~/Views/Shared/_EmployeeMain.cshtml";
}

<head>
    <script src="~/Scripts/jquery-1.10.2.js"></script>
    <script src="~/Scripts/jquery-ui.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
    <script src="~/Scripts/jquery.validate.js"></script>
</head>

<body>

    <span class="content_h4">@Resources.Localization.company_info</span>
    <br />
    <br />

    <div id="FormContainer">
        @Html.Partial("Partial/_CompanyInfo", Model)
    </div>


</body>

Partial Markup:

    @model BTGHRM.Models.CompanyMainInfoModel

@using (Html.BeginForm("CompanyInfo", "Administration"))
{
    <table>
        <tr>
            <td>@Html.LabelFor(m => m.CompanyName)</td>
            <td>@Html.TextBoxFor(m => m.CompanyName)</td>
            <td>@Html.ValidationMessageFor(m => m.CompanyName, "" , new { @class="text-danger"})</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(m => m.CompanyAddress)</td>
            <td>@Html.TextBoxFor(m => m.CompanyAddress)</td>
            <td>@Html.ValidationMessageFor(m => m.CompanyAddress, "", new { @class = "text-danger" })</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(m => m.CompanyDirector)</td>
            <td>@Html.TextBoxFor(m => m.CompanyDirector)</td>
            <td>@Html.ValidationMessageFor(m => m.CompanyDirector, "", new { @class = "text-danger" })</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(m => m.CompanyTelephoneNumber)</td>
            <td>@Html.TextBoxFor(m => m.CompanyTelephoneNumber)</td>
            <td>@Html.ValidationMessageFor(m => m.CompanyTelephoneNumber, "", new { @class = "text-danger" })</td>
        </tr>
    </table>
    <input style="width:78px" type="submit" [email protected] />
}

And despite [Required] it still allows me to set null values to any field. What is wrong with my code?

Upvotes: 0

Views: 53

Answers (2)

You have to have unobtrusive scripts enabled in web config

<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

Then you need to add

  • jquery
  • jquery.validate.js
  • jquery.validate.unobtrusive.js

Upvotes: 2

vikram singh
vikram singh

Reputation: 69

You need to add "jquery.validate.js" and "jquery.validate.unobtrusive.js" in your View. Validation needs the above two files along with "Jquery.js". Hope this helps.

Upvotes: 2

Related Questions