使用基于远程属性的验证时必须双重提交

发布于 2024-10-19 23:24:02 字数 128 浏览 1 评论 0原文

我们的模型上有一个具有 [Remote] 属性的字段。当我们将该字段存储在隐藏表单元素上然后尝试提交该表单时,我们必须单击提交按钮两次。同样有趣的是,我们第二次单击它时,没有发生远程验证(Fiddler 是这么说的)。

想法?

We have a field on our model which has a [Remote] attribute. When we store that field on a Hidden form element and then try to submit that form we have to click the submit button twice. Also interesting is that the 2nd time we click it no remote validation is occurring (so says Fiddler).

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白龙吟 2024-10-26 23:24:02

无法重现。如果隐藏字段用 Remote 属性修饰,则无论您单击提交按钮多少次,如果远程函数发送 false,您都将无法提交表单。

例如:

Model:

public class MyViewModel
{
    [HiddenInput(DisplayValue = false)]
    [Remote("Check", "Home")]
    public string Id { get; set; }

    [Required]
    public string Name { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel
        {
            Id = "1"
        });
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }

    public ActionResult Check(string Id)
    {
        return Json(Id == "2", JsonRequestBehavior.AllowGet);
    }
}

View:

@model AppName.Models.MyViewModel

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

@using (Html.BeginForm())
{
    @Html.EditorForModel()
    <input type="submit" value="OK" />
}

因为远程函数总是返回 false,所以无法提交该表单。如果远程函数返回 true,那么单击一次就足以提交它,当然假设其他验证已通过。

Unable to repro. If the hidden field is decorated with the Remote attribute you won't be able to submit the form no matter how many times you click on the submit button if the remote function sends false.

For example:

Model:

public class MyViewModel
{
    [HiddenInput(DisplayValue = false)]
    [Remote("Check", "Home")]
    public string Id { get; set; }

    [Required]
    public string Name { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel
        {
            Id = "1"
        });
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }

    public ActionResult Check(string Id)
    {
        return Json(Id == "2", JsonRequestBehavior.AllowGet);
    }
}

View:

@model AppName.Models.MyViewModel

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

@using (Html.BeginForm())
{
    @Html.EditorForModel()
    <input type="submit" value="OK" />
}

Because the remote function will always return false this form cannot be submitted. If the remote function returns true a single click would be enough to submit it assuming of course that the other validation passed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文