繁体   English   中英

在MVC 4提交按钮中禁用客户端验证

[英]Disable client-side validation in MVC 4 submit button

在我的asp.net mvc4应用程序中,例如在创建新项目“ cond”时,出现此错误“从客户端检测到潜在危险的Request.Form值”。 cond是数据库中的一个表,具有两个字段“ id_regle”和“ cond_txt”。 cond_txt字段将具有以下形式的条件:“ left_side + op + right_side”; op = <,>,==...。我尝试了发现的不同建议,但是没有一个建议对我有用以禁用此验证。

这是视图“创建”:

@model MvcApplication3.cond
@{
ViewBag.Title = "Create";
}
 <h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>cond</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.cond_txt)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.cond_txt)
        @Html.ValidationMessageFor(model => model.cond_txt)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.id_regle, "regle")
    </div>
    <div class="editor-field">
        @Html.DropDownList("id_regle", String.Empty)
        @Html.ValidationMessageFor(model => model.id_regle)
    </div>

    <p>
        <input type="submit" value="Create" class="cancel" />
    </p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

这是控制器:

 public ActionResult Create()
    {
        ViewBag.id_regle = new SelectList(db.regle, "id", "action");
        return View();
    }

    //
    // POST: /Default2/Create

    [HttpPost]
    public ActionResult Create(cond cond)
    {
        if (ModelState.IsValid)
        {
            db.cond.AddObject(cond);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.id_regle = new SelectList(db.regle, "id", "action", cond.id_regle);
        return View(cond);
    }

我尝试在web.config中添加validateRequest =“ false”,但这对我不起作用任何建议

这对我有用,我在控制器参考中添加了[ValidateInput(false)]

您可以在控制渲染中使用以下禁用客户端验证。

@{ Html.EnableClientValidation(false); }

或尝试这个

<%@ Page validateRequest="false" %> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM