MVC 远程属性 - 提交问题

发布于 01-07 15:36 字数 2923 浏览 1 评论 0原文

我正在尝试检查 typeId 和 typeId 的组合是否为SupplementId 已存在于我的数据库中。所以我在我的 SupplementId 中添加了一个远程属性来执行此操作。我的问题是验证应该在更改任一下拉列表时触发。我用 ddl onchange 上的 javascript 修复了这个客户端。但现在我无法发布我的表格。当我单击该按钮时,它会聚焦最后一个下拉列表,但不会显示任何错误,并且控制器中的 Post 方法不会被触发。如果我在 chrome 控制台中执行 form.submit() ,该方法会被调用,但验证会被忽略。以前有人遇到过这个问题吗?

顺便说一句,此代码已被清理到最少,因此标签、错误消息等可能会丢失, 这些不应该是问题。

部分视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SupplementTypeClass>" %>

<script src="<%: Url.Content("~/Scripts/jquery-1.5.1.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>"
    type="text/javascript"></script>
<% using (Html.BeginForm("AddEditSupplementType", "Supplement"))
   { %>
<%: Html.ValidationSummary(true)%>
<fieldset>

    <legend>
        Add type
    </legend>
    <div class="editor-content">
        <div class="editor-label">
            <%: Html.LabelFor(model => model.SupplementId)%>
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.SupplementId, new SelectList(ViewBag.Supplements, "SupplementId", "Name", Model.SupplementId), "Select supplement", new { onchange = "$('#ddl').removeData('previousValue');$('#ddl').valid();" })%>
            <%: Html.ValidationMessageFor(model => model.SupplementId)%>
        </div>
    </div>
    <div class="editor-content">
        <div class="editor-label">
            <%: Html.LabelFor(model => model.TypeId)%>
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.TypeId, new SelectList(ViewBag.Types, "TypeId", "Name", Model.TypeId), "Select type", new { id ="ddl" })%>
            <%: Html.ValidationMessageFor(model => model.TypeId)%>
        </div>
    </div>
    <div class="editor-buttons">
        <input type="submit" value="Save" />
    </div>
</fieldset>
<% } %>

控制器:

[HttpPost]
        public ActionResult AddEditSupplementType(SupplementTypeClass sup)
        {
            if (ModelState.IsValid)
            {
                //code to insert to DB
            }
            return RedirectToAction("FicheAddEditSupplementType", new { supplementTypeId = sup.supplementTypeId});
        }

类:

public class SupplementTypeClass
{

    #region Members

    public int SupplementId { get; set; }

    [RemoteAttribute("SupplementTypeCheck", "Supplement", AdditionalFields = "SupplementId")]
    public int TypeId { get; set; }

    #endregion
}

I'm trying to check if the combination of typeId & supplementId already exists in my database. So I've added a remoteattribute to my supplementId which does this. My problem is that the validation should trigger on changing either dropdownlist. I kinda fixed this clientside with the javascript on the ddl onchange. But now I'm not able to post my form. When I click the button, it focuses the last dropdownlist but doesn't show any errors and my Post method in the controller doesn't get fired. If I execute a form.submit() in chrome console the method gets called but validation is ignored. Anyone had this issue before?

BTW this code is cleaned to a minimum so labels, errormessages, etc could be missing,
these shouldn't be the issue.

Partial view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SupplementTypeClass>" %>

<script src="<%: Url.Content("~/Scripts/jquery-1.5.1.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>"
    type="text/javascript"></script>
<% using (Html.BeginForm("AddEditSupplementType", "Supplement"))
   { %>
<%: Html.ValidationSummary(true)%>
<fieldset>

    <legend>
        Add type
    </legend>
    <div class="editor-content">
        <div class="editor-label">
            <%: Html.LabelFor(model => model.SupplementId)%>
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.SupplementId, new SelectList(ViewBag.Supplements, "SupplementId", "Name", Model.SupplementId), "Select supplement", new { onchange = "$('#ddl').removeData('previousValue');$('#ddl').valid();" })%>
            <%: Html.ValidationMessageFor(model => model.SupplementId)%>
        </div>
    </div>
    <div class="editor-content">
        <div class="editor-label">
            <%: Html.LabelFor(model => model.TypeId)%>
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.TypeId, new SelectList(ViewBag.Types, "TypeId", "Name", Model.TypeId), "Select type", new { id ="ddl" })%>
            <%: Html.ValidationMessageFor(model => model.TypeId)%>
        </div>
    </div>
    <div class="editor-buttons">
        <input type="submit" value="Save" />
    </div>
</fieldset>
<% } %>

Controller:

[HttpPost]
        public ActionResult AddEditSupplementType(SupplementTypeClass sup)
        {
            if (ModelState.IsValid)
            {
                //code to insert to DB
            }
            return RedirectToAction("FicheAddEditSupplementType", new { supplementTypeId = sup.supplementTypeId});
        }

Class:

public class SupplementTypeClass
{

    #region Members

    public int SupplementId { get; set; }

    [RemoteAttribute("SupplementTypeCheck", "Supplement", AdditionalFields = "SupplementId")]
    public int TypeId { get; set; }

    #endregion
}

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

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

发布评论

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

评论(1

孤独患者2025-01-14 15:36:53

Remote 属性默认使用 GET 方法(而不是 POST)执行 Ajax 调用。

让您在模型中指定 POST 方法

[RemoteAttribute("SupplementTypeCheck", "Supplement", AdditionalFields = "SupplementId", HttpMethod="POST")]

The Remote attribute does Ajax calls using the GET method by default, not POST.

Make your you specificy the POST method in your model

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