0

I am trying to make a View field required, but for some reason it isnt working (I can hit Submit and it POST's the form) without being forced to select a value. What am I missing?

View (CardCreate.cshtml)

@model xxx.Models.TicketsViewModels.CardCreateViewModel

<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>

<div>
    @Html.LabelFor(m => m.Title)
    @Html.TextBoxFor(m => m.Title)
    @Html.ValidationMessageFor(m => m.Title)
</div>

ViewModel (CardCreateViewModel.cs)

[Required(ErrorMessage = "Title is required")]
[Display(Name = "Title")]
public string Title { get; set; }

Controller (TicketsController.cs)

[HttpPost]
public async Task<ActionResult> CardCreate(CardCreateViewModel c)
{
    if (!this.ModelState.IsValid)
    {
        return View("CardCreate", c);
    }
    //adding other data

}

Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • How is your controller's code? Post more code, form, action on controller, viewModel type you are using... – Felipe Oriani May 07 '18 at 20:27
  • you talking about client-side validation or server-side? Please post your controller action thats being posted to. Do you have `ModelState.IsValid` check? – zgood May 07 '18 at 20:30
  • Have you taken a look at this answer: https://stackoverflow.com/a/17399219/82208 – aquinas May 07 '18 at 20:41
  • Have you included the relevant scripts for client side validation? –  May 07 '18 at 21:42

1 Answers1

0

ASP.NET MVC's validation is not client-side, it's server-side. Nothing will stop the browser from submitting it. Your controller actions need to return the HTML view back to the browser with the validation message displayed:

[HttpGet]
public ActionResult Index()
{
    return this.View( new IndexViewModel() );
}

[HttpPost]
public ActionResult Index( IndexViewModel model )
{
    // Immediately prior to execution entering this `Index` action method, the ASP.NET MVC runtime will populate the `Controller.ModelState` object with information about validation errors in `model`.

    if( !this.ModelState.IsValid )
    { 
        // When the View is rendered the `ValidationMessageFor` helper will inspect the ModelState and show the error message itself, so no custom logic is needed here
        return this.View( model );
    }

    // TODO: Do processing of valid data here

    // Upon successful processing/saving/etc, redirect back to the GET view:
    return this.RedirectToAction( nameof(this.Index) );
}

ASP.NET MVC does come with some scripts for client-side validation but it's optional and not as integral to the platform as the Validation-controls were in ASP.NET WebForms. However, client-side validation is only for the convenience of your visitors: it does not add any actual security, and visitors with JavaScript disabled will not see client-side validation messages. Never trust the client!

Dai
  • 141,631
  • 28
  • 261
  • 374