【问题标题】:How can i validate from jquery form submit ?如何从 jquery 表单提交进行验证?
【发布时间】:2014-08-15 21:49:33
【问题描述】:

我无法在视图上启用验证消息,因为我使用 jquery 提交表单。

任何人都可以对使用 jquery 提交表单进行验证有什么想法吗?

查看

@using (Html.BeginForm("Index", "Sample", FormMethod.Post, new{ id="frmStep1"}))
 {
 <div class="form-group">
     <label>Question 1:<span class="text-danger">*</span></label>
     <small class="text-danger">
         <i> @Html.ValidationMessageFor(model => model.answer_01)</i>
     </small>
     <div class="radio">
         <label>
             @Html.RadioButtonFor(m=>m.answer_01,"2355")Male
         </label>
     </div>
     <div class="radio">
         <label>
             @Html.RadioButtonFor(m=>m.answer_01,"2355")Male
         </label>
     </div>
  </div>
  <button class="btn btn-primary" type="button" id="btnStep1">
     <span class="fa fa-arrow-circle-right">
     </span> Save
  </button>
}

脚本

$("#btnStep1").click(function () {

        $.ajax({
            url: "../Profile/Question",
            type: "POST",
            dataType: "json",
            data: $("form#frmStep1").serialize(),
            success: function(data) {
                alert('Ok');

            },
            fail: function(data) {
                console.log('failed to submit.');
            }
        });
    });

控制器

[HttpPost]
public ActionResult Index(SampleViewModel model)
{
    return Json("success",JsonRequestBehavior.AllowGet);
}

当用户无法输入某些文本时,我不知道如何启用 ViewModel 上的验证器,请提供一些解决这些解决方案的想法。

【问题讨论】:

  • 模型验证在 ja​​vascriptvalidations 之后工作。如果您希望您的模型验证工作,那么您必须将输入类型作为提交,或者您必须在提交之前验证 Javascript 中的每个字段

标签: jquery asp.net asp.net-mvc-4 validation


【解决方案1】:

您可以在Form中指定您的动作名称和控制器名称,并将输入类型为“提交”,如下所示:

 @using (Html.BeginForm("YourActionName", "YourControllerName", FormMethod.Post, new{ id="frmStep1"}))

   {
     <div class="form-group">
        <label>Question 1:<span class="text-danger">*</span></label>
        <small class="text-danger">
         <i> @Html.ValidationMessageFor(model => model.answer_01)</i>
      </small>
      <div class="radio">
       <label>
          @Html.RadioButtonFor(m=>m.answer_01,"2355")Male
        </label>
       </div>
      <div class="radio">
       <label>
          @Html.RadioButtonFor(m=>m.answer_01,"2355")Male
       </label>
      </div>
    </div>
 <input class="btn btn-primary" type="submit" value="Save" id="btnStep1" />

}

【讨论】:

    猜你喜欢
    • 2018-11-16
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 2021-09-08
    • 1970-01-01
    • 2015-07-26
    • 2018-06-12
    相关资源
    最近更新 更多