Reputation: 13
Is there a simple way to disable model validation on disabled input fields? My issue is I have a form with model validation I'm using jquery to disable an input and hide it and using Ajax to submit the form but it still does model validation on that field when it's disabled I'm using html 5 validation for client side and model validation for server side. Any help would be greatly appreciated. This is only part of my code its much bigger but I just wanted to know if there was a simple way to disable model validation on a disabled input field. Not sure if you will be able to tell how it works with my code examples I tried to simplify them to get the point across but I think I might have slaughtered it. I remove the required html 5 and disabled from the location text box to test for the model validation which works great but I don't want it to work when the location text box is disabled.
[Required(ErrorMessage = "You must enter a location")]
[StringLength(15, ErrorMessage ="Must Be Under 15 Characters")]
public string location_txt_box { get; set; }
[HttpPost]
[ValidateAjax]
public JsonResult AddData(form_model form_data)
{
return Json(form_data);
}
public class ValidateAjaxAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.HttpContext.Request.IsAjaxRequest())
return;
var modelState = filterContext.Controller.ViewData.ModelState;
if (!modelState.IsValid)
{
var errorModel =
from x in modelState.Keys
where modelState[x].Errors.Count > 0 select new
{
key = x,
errors = modelState[x].Errors.Select(y => y.ErrorMessage).ToArray()
};
filterContext.Result = new JsonResult()
{
Data = errorModel
};
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
}
}
}
<section class="u_select_section">
<center>
<label>
@Html.RadioButtonFor(m => m.user_department_radio_group,"user", htmlAttributes: new{
@id = "u_select"
})
Filling out for myself
</label>
</center>
</section>
<section id="location_display_section" class="location_display_section">
<div id="location_error"></div>
@Html.TextBoxFor(m => m.location_txt_box, htmlAttributes: new{
@id = "dep_loca_id",
@disabled = "disabled",
@placeholder = "Type your Location",
@required = "required"
})
</section>
$('#request_form').on('submit', function (e) {
$.ajax({
type: "POST",
url: "../Home/AddData",
datatype: "json",
data: $("#request_form").serializeArray(),
beforeSend: function () {
$("#progress").show();
},
complete: function () {
$('#progress').hide("fade",2000);
},
success: function (data) {
alert("Success")
},
error: function (data) {
$("#location_error").show();
var response = JSON.parse(data.responseText);
var location_error = response[0].errors[0];
$("#location_error").text("" + location_error + "").css({ "color": "red" });
}
});
e.preventDefault();
});
//disable functions
function disable_function_1(i) {
return $(i).attr("disabled", "disabled");
}
//user and department radio button click functions
$("#u_select").click(function () {
$(dep_loca_id).hide(effect1);//hide the department location text box
disable_function_1(dep_loca_id);//disable the department location text box
});
Upvotes: 0
Views: 2028
Reputation: 3502
you can remove specific field validation with ModelState.Remove()
.
[HttpPost]
public ActionResult YourAction(YourModel model)
{
// your condition here
ModelState.Remove("location_txt_box "); // to omit specific field according to condition
if (ModelState.IsValid)
{
// your code here
}
return View();
}
Upvotes: 1