远程验证DropDownList,MVC3,在我的情况下不触发 [英] Remote Validate for DropDownList, MVC3, not firing in my case

查看:107
本文介绍了远程验证DropDownList,MVC3,在我的情况下不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC3和EF 4.1,我的模型中有两个DropDownList,这是必需的,也不能重复.我想要远程验证功能:当用户提交数据时,ValidateDuplicateInsert会被触发.但是我无法启动ValidateDuplicateInsert函数.我怎么了紧急,请帮忙!非常感谢!

我的模特

I am using ASP.NET MVC3 and EF 4.1 I have two DropDownList in my Model, It is required and not duplicated too. And I want the Remote validate function: ValidateDuplicateInsert get firing when user submit data. But I can NOT get the ValidateDuplicateInsert function firing. What am I wrong? It ugent, please help! Thank you so much!

My Model

[Key]
    public int CMAndOrgID { get; set; }

    [Display(Name = "CM")]
    [Required(ErrorMessage = "CM is required.")]
    [Remote("ValidateDuplicateInsert", "CMAndOrg", HttpMethod = "Post", AdditionalFields = "CMID, OrganizationID", ErrorMessage = "CM is assigned to this Organization.")]
    public int? CMID { get; set; }

    [Display(Name = "Organization")]
    [Required(ErrorMessage = "Organization is required.")]
    public int? OrganizationID { get; set; }

    public virtual CM CM { get; set; }
    public virtual Organization Organization { get; set; }



我的CMAndOrg控制器中的ValidateDuplicateInsert函数



The ValidateDuplicateInsert function in my CMAndOrg controller

[HttpPost]
    public ActionResult ValidateDuplicateInsert(string cmID, string orgID)
    {
        bool flagResult = true;
        foreach (CMAndOrg item in db.CMAndOrgs)
        {
            if (item.CMID.ToString() == cmID && item.OrganizationID.ToString() == orgID)
            {
                flagResult = false;
                break;
            }
        }
        return Json(flagResult);
    }



还有我的看法



And my View

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>CMAndOrg</legend>

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

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

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

推荐答案

1.在CMID上写[Remote]时,不需要在其他字段"中提及它.默认情况下,CMID将传递给操作方法.
2.如下更改退货声明,它将起作用.
返回Json(flagResult,JsonRequestBehavior.AllowGet);

让我知道它是如何工作的...
1. When you write [Remote] on CMID, you don''t need to mention it in Additional Fields.... by default, CMID will be passed to action method.
2. Change your return statement as below, it will work.
return Json(flagResult, JsonRequestBehavior.AllowGet);

let me know how it worked...


MVC3中的一个错误与dropdownlist上的非侵入式验证有关.请参考此http://aspnet.codeplex.com/workitem/7629[^]链接以获取更多详细说明.

简要地说,您不能在类别集合和类别字段中使用相同的名称,因此只需更改集合名称并在视图中更新以下行
There is a bug in MVC3 related to unobtrusive validation on dropdownlist. Please reference to this http://aspnet.codeplex.com/workitem/7629[^] link for more detail explaination.

Briefly, you can''t use the same name for category collection and category field, so just change your collection name and update following line in your view
@Html.DropDownList("CategoryID", String.Empty)


与此


with this

@Html.DropDownListFor(model => model.CategoryID, new SelectList((System.Collections.IEnumerable)ViewData["Categories"], "Value", "Text"))



再次感谢Henry He

原始链接
ASP.NET MVC3验证基础 [ ^ ]



Thanks again Henry He

Original link
ASP.NET MVC3 Validation Basic[^]


这篇关于远程验证DropDownList,MVC3,在我的情况下不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆