BlowFish BlowFish - 9 months ago 73
jQuery Question

Unable to require wysiwyg textbox MVC

I have it required on the back-end and the front-end and yet I can still submit the form...

View Model

public string ContentText { get; set; }


@Html.TextAreaFor(i => i.ContentText, new Dictionary<string, object> { { "class", "form-control" }, { "rows", 4 }, { "id", "formContent" }, { "name", "ContentText" }, { "data-bind", "value:formContent" }, { "required", "required" } })



Answer Source

I'm unfamiliar with ckeditor, but in MVC the [Required] attribute is only part of the equation.

If you're using EntityFramework, calling SaveChanges() on the database would throw an exception if the model is invalid.

For submissions to the controller, you want to check if (ModelState.IsValid) { //do something } - this is false if there are any validation errors. Note you can also check if (ModelState.IsValidField("myField")) { //do something } if you don't care about the whole model.

Whatever code is processing your submit needs to handle the invalid model. You can either do this in JS or C#.