Ctrl_Alt_Defeat
Ctrl_Alt_Defeat

Reputation: 4009

MVC 5 IValidatableObject not displaying error message?

I am following ScottGus blog here - The Basic requirement I have is that I have two text fields on screen - I want to validate if both of them are blank and Sumite button is pressed I want to display to use that one or the other is required

I have a UserModel as below:

public class UserViewModel : IValidatableObject
{
    [Display(Name = "Name:")]
    public string UserName { get; set; }

    [Display(Name = "User Account Number:")]
    public string UserAccountNumber { get; set; }

    public bool UserHasName()
    {
        return !string.IsNullOrEmpty(UserName);
    }

    public bool UserHasAccountNumber()
    {
        return !string.IsNullOrEmpty(UserAccountNumber);
    }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (!UserHasName() && !UserHasAccountNumber())
        {
            yield return new ValidationResult("Please enter an Account Number or Name ", new [] {"UserName"});
        }
    }
}

On My view I have:

  @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", @placeholder = "UserName" })
                    @Html.ValidationMessageFor(m => m.UserName)

Within Webconfig I have the following :

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

However when I hit submit button with both fields blank no error message is displaying on screen?

Update

Within Bundle config I am also using jQuery.validate.unobtrusive:

 bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js",
                        "~/Scripts/jquery.validate.js",
                        "~/Scripts/jquery.validate.unobtrusive.js"));

Although afterwards I include js for bootstrap and kendo ui - could that be causing some conflict?

Updated Two

Controller code:

public ActionResult UserDetails()
    {
        if (ModelState.IsValid)
        {
            // IsValid is true so code goes in here when text fields are blank??
            //redirect to another view
        }

        return View();
    }

I do have a JsonValididationFilterAttribute class - code as below - although as I say for some reason breakpoint on Validate or this OnActionExecuting is not being hit?

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (actionContext.ModelState.IsValid) return;
        var errorMessages = actionContext.ModelState.Values
            .SelectMany(modelState => modelState.Errors);
        actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, Json.Encode(errorMessages));
    }

Upvotes: 2

Views: 2733

Answers (2)

Boris Parfenenkov
Boris Parfenenkov

Reputation: 3279

I think you should insert parameter in your action method, like this:

public ActionResult UserDetails(UserViewModel model)

Otherwise, MVC didn't understand, that you want to call Validate on this model.

Upvotes: 1

Stefan
Stefan

Reputation: 17658

You might need some additional javascript:

include jquery.unobtrusive-ajax.js file.

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

You can find it at NuGet Packages: Microsoft.jQuery.Unobtrusive.Ajax.

Upvotes: 0

Related Questions