【问题标题】:How to handle Remote Attributes on Model Grid Edit? ASP.Net MVC3如何处理模型网格编辑上的远程属性? ASP.Net MVC3
【发布时间】:2014-02-17 20:22:03
【问题描述】:
public class Page
{
 [Remote("CheckDuplicate", "Home", ErrorMessage = "Name already taken")]
 public string Name { get; set; }
}

在控制器中,我根据“检查”结果返回 JsonResult 数据,如下所示:

public JsonResult CheckDuplicate(string Name )
{
  var result = db.abc.Where(a => a.Name == Name ).Count() == 0;
  return Json(result, JsonRequestBehavior.AllowGet);
}

这在添加新名称时工作正常,但问题是,它限制我编辑,因为它正在检查相同的查询。 我正在使用 Telerik Grid ASP.NET MVC 并使用 (GridEditMode.PopUp) 方法

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 telerik telerik-mvc


    【解决方案1】:

    在您的代码中进行这些修改。

    public class Page
    {
        [DefaultValue(0)]
        public int ID { get; set; }
        [Remote("CheckDuplicate", "Home", AdditionalFields = "ID", ErrorMessage = "Name already taken")]
        public string Name { get; set; }
    }
    

    在控制器中

    public JsonResult CheckDuplicate(string Name, int Id)
    {
      var result = db.abc.Where(a => (Id == 0) ? (a.Name == Name && a.ID != Id) : (a.Name == Name)).Count() == 0;
      return Json(result, JsonRequestBehavior.AllowGet);
    }
    

    您可以使用更多附加字段并用逗号分隔它们。 例如:AdditionalFields = "ID, ID2

    "

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多