Jquery 为禁用的表单输入禁用模型验证的简单方法?
有没有一种简单的方法可以在禁用的输入字段上禁用模型验证?我的问题是,我有一个带有模型验证的表单,我使用jquery禁用输入并隐藏它,并使用Ajax提交表单,但禁用该表单后,它仍然在该字段上进行模型验证,我在客户端使用HTML5验证,在服务器端使用模型验证。任何帮助都将不胜感激。这只是我代码的一部分,它要大得多,但我只是想知道是否有一种简单的方法可以在禁用的输入字段上禁用模型验证。我不确定您是否能够通过我的代码示例来说明它是如何工作的,我试图简化它们以让大家明白这一点,但我想我可能已经彻底搞糟了。我从位置文本框中删除了所需的HTML5并将其禁用,以测试模型验证,这非常有效,但我不希望在位置文本框被禁用时它能工作 模型的一部分 控制器的一部分 部分视图 用于隐藏和禁用的JqueryJquery 为禁用的表单输入禁用模型验证的简单方法?,jquery,asp.net-mvc,Jquery,Asp.net Mvc,有没有一种简单的方法可以在禁用的输入字段上禁用模型验证?我的问题是,我有一个带有模型验证的表单,我使用jquery禁用输入并隐藏它,并使用Ajax提交表单,但禁用该表单后,它仍然在该字段上进行模型验证,我在客户端使用HTML5验证,在服务器端使用模型验证。任何帮助都将不胜感激。这只是我代码的一部分,它要大得多,但我只是想知道是否有一种简单的方法可以在禁用的输入字段上禁用模型验证。我不确定您是否能够通过我的代码示例来说明它是如何工作的,我试图简化它们以让大家明白这一点,但我想我可能已经彻底搞糟了。
您可以使用ModelState.remove删除特定字段验证
发布一些可能有效的示例代码只是想知道文本框是否被禁用,只是不确定我是否可以检查mvc和c的新功能。我会尝试一下。什么时候禁用文本框输入?是否能够理解您的用户和部门单选按钮是否被使用您的模型点击?在我的模型中,我有它们被设置为单选按钮的字符串。起初,我把它们设置为布尔,但我没有从它们那里得到我想要的东西。当我放置断点以查看模型将发生什么情况时,我会得到该值。它的用户或部门取决于选择的用户或部门。当用户选择用户无线电按钮时,我会禁用文本框。因此,您可以通过在ModelState之前检查模型来添加条件。删除
[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
});
[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();
}