必填字段和客户端验证

时间:2022-04-22 15:34:40

i have put [Required(ErrorMessage = "*")] in model

我把[Required(ErrorMessage =“*”)]放在模型中

[Required(ErrorMessage = "*")]
[Display(Name = "Amount")]
public decimal Amount { get; set; }

set these field true in webconfig

在webconfig中将这些字段设置为true

 <add key="ClientValidationEnabled" value="true" />
 <add key="UnobtrusiveJavaScriptEnabled" value="true" />

and

 @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)

               <tr>
                    <td>@Html.LabelFor(model => model.Amount)</td>
                    <td>@Html.EditorFor(model => model.Amount )
                @Html.ValidationMessageFor(model => model.Amount )</td>

                        <input onclick="return validate();" style=" margin:20px; width:120px; height:40px;" type="submit" value="ثبت فیش" />

    }

button click not check validation , add this to controller for serverside validation

按钮单击不检查验证,将其添加到控制器以进行服务器端验证

 if (ModelState.IsValid)
            {
                obj.DateAdded = DateTime.Now;
                obj.OrderID = id;
                RingSportEntities objEntity = new RingSportEntities();
                objEntity.Payments.Add(obj);
                var p = (from o in objEntity.Orders where o.OrderId == id select o).First();
                p.Status = 2;
                objEntity.SaveChanges();
                return View();
            }
            else { return View(obj); }

now , it works serverside verfication , why client side now works ?

现在,它运行服务器端验证,为什么客户端现在工作?

2 个解决方案

#1


0  

Try like this,

试试这样,

View

视图

 @using (Html.BeginForm("YourPage", "Controller", FormMethod.Post, new { id = "FrmIndex" }))
        { 
<input type="submit" value="Save" style="float: left;" id="btnSave" title="btn" />
    }

Script

脚本

<script type="text/javascript">
        $(document).ready(function () {


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

                var validation = $("#FrmIndex"); // My From Id
                if (!validation.valid()) {
                    return false;
                }

            });
        });
    </script>

You have to add this two script fro validation.

你必须添加这两个脚本来验证。

<script src="@Url.Content("~/Script/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Script/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

#2


1  

try this

尝试这个

<input onsubmit="return $(this).closest("form").valid();" style=" margin:20px; width:120px; height:40px;" type="submit" value="submit!" />

#1


0  

Try like this,

试试这样,

View

视图

 @using (Html.BeginForm("YourPage", "Controller", FormMethod.Post, new { id = "FrmIndex" }))
        { 
<input type="submit" value="Save" style="float: left;" id="btnSave" title="btn" />
    }

Script

脚本

<script type="text/javascript">
        $(document).ready(function () {


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

                var validation = $("#FrmIndex"); // My From Id
                if (!validation.valid()) {
                    return false;
                }

            });
        });
    </script>

You have to add this two script fro validation.

你必须添加这两个脚本来验证。

<script src="@Url.Content("~/Script/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Script/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

#2


1  

try this

尝试这个

<input onsubmit="return $(this).closest("form").valid();" style=" margin:20px; width:120px; height:40px;" type="submit" value="submit!" />