【问题标题】:ASP.NET MVC RemoteAttribute does not trigger action method in controllerASP.NET MVC RemoteAttribute 不会触发控制器中的操作方法
【发布时间】:2014-12-30 03:24:28
【问题描述】:

1.项目规范

任务:检查用户是否输入了唯一的字典名称。

模型(我项目中的所有实体都在单独的类中):

public class Dictionary
{
    [Required]
    [Index("IX_Dictionary_Name_UK", IsUnique = true)]
    [MaxLength(100)]
    [Display(Name="Name")]
    [Remote("DictionaryNameExists", "Administrator", ErrorMessage = "Dictionary name already exists in the database!")]
    public string Name { get; set; }        
}

控制器:

[Authorize(Roles="Administrator")]
public class AdministratorController : BaseController
{

    public AdministratorController(IDictionaryRepository dictionaryRepository)
    {
        this.dictionaryRepository = dictionaryRepository;
        this.dictionaryModel = new DictionaryModel(); 
    }

    [AllowAnonymous]
    public JsonResult DictionaryNameExists(string Name)
    {
        return Json(dictionaryRepository.DictionaryNameExists(Name), JsonRequestBehavior.AllowGet);
    } 

}

查看:

@using (Html.BeginForm("AddEditDictionary", "Administrator")){
<div id="dictionaryContainer" class="modal-body">

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

</div>
<div class="modal-footer">
    <button id="btnSaveChanges" type="submit" name="command" value="Save" class="btn btn-primary">Save dictionary</button>
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>}

Above View 是一个 PartialView,我通过 ajax 查询将其包含在引导模式中,如下所示:

function MakeGetRequest() {
    var url = "/Administrator/AddDictionary/";

    $.get(url, function (data) {
        $("#containerId").html(data);
        $("#modalId").modal('show');
    });
}

2。问题

DictionaryNameExists 方法未触发。我在那里设置了断点,它没有“击中”它。其次,即使我输入了现有名称,ModelState.IsValid 也始终为真。 ModelState.IsValid 适用于 MaxLengthAttribute 和 RequiredAttribute。

3. Whathaveyoutried.com

  • 我已在 web.config 中启用设置(ClientValidationEnabled 和 UnobtrusiveJavaScriptEnabled)
  • 我在控制器中添加了如下属性:

    [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]

  • 我已将 HttpGet 属性添加到方法和 RemoteAttribute 中。

  • 我已经包含了 javascript 库
  • 服务器正确生成了我的标签:

... data-val-remote="数据库中已经存在字典名!" data-val-remote-additionalfields="*.Name" data-val-remote-url="/Administrator/DictionaryNameExists"...

  • 正如Darin 回答的那样,我已将他的代码添加到我的“MakeRequestFunction”中
  • 我终于清除了浏览器 (Chrome) 缓存。
  • 我的 ViewModel 是一个包含我的 Entity 类的类,所以我的想法是绑定我的属性,但它也没有为我工作

4.现在呢?

我可以制作自定义属性,但我在单独的库中有模型,所以我不能在那里注入我的存储库来检查唯一性。出于同样的原因,我不能使用FluentValidation 这样的库。我相信我的 Action 没有触发的原因很简单。我正在寻求您的帮助。

【问题讨论】:

  • 你已经用 Authorize 属性装饰了控制器并且 DictionaryNameExists 也在这个控制器中,所以这不应该是一个问题吗?
  • 这是我的第一个想法,所以我在我的方法中使用了 [AllowAnonymous]。它没有帮助。但是感谢您的宝贵时间。

标签: c# asp.net-mvc entity-framework validation


【解决方案1】:

因为您的内容是在验证调用之后加载的。您需要在加载局部视图后调用验证和解析。

$.get(url, function (data) {
    $("#containerId").html(data);
    $("#modalId").modal('show');
    var form = $('form');
    form.removeData('validator');
    form.removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse(form);
});

【讨论】:

  • 谢谢它解决了我的问题。所以这是我的错误。我确实从验证器中删除了数据,但在 $.get(url, function (data){});不在这个结构内。
猜你喜欢
  • 1970-01-01
  • 2019-03-30
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
相关资源
最近更新 更多