.net 如何在编辑时验证EF4中的重复条目

.net 如何在编辑时验证EF4中的重复条目,.net,asp.net-mvc,c#-4.0,entity-framework-4,.net,Asp.net Mvc,C# 4.0,Entity Framework 4,我有这个代码来检查城市的重复条目 public ActionResult Create(City city) { var CityCheck= context.Cities.Where(u => u.CityName == city.CityName && u.ContId = city.ContId).FirstOrDefault(); if (CityCheck == null)

我有这个代码来检查城市的重复条目

public ActionResult Create(City city)
        {

            var CityCheck= context.Cities.Where(u => u.CityName == city.CityName && u.ContId = city.ContId).FirstOrDefault();

            if (CityCheck == null)
           {
                 context.Cities.Add(city);
            }
            else
            {
                ModelState.AddModelError("CityName", "City name already exists.");

            }

            if (ModelState.IsValid)
            {
                context.Cities.Add(city);
                // rest of Code
                context.SaveChanges(city);
                return RedirectToAction("Index");
            }
            ViewBag.PossibleCountries = context.Countries.Where(f => f.IsActive == true && f.IsDeleted == false).ToList();

            return View(city);
        }
它工作得非常好,下面是我的编辑操作结果方法

    [HttpPost]
    public ActionResult Edit(City city)
    {

        var CityCheck = context.Cities.Where(u => u.CityName == city.CityName && u.ContId == city.ContId).FirstOrDefault();
        if (CityCheck == null)
        {
            context.Entry(city).State = EntityState.Modified;
        }
        else
        {
            ModelState.AddModelError("CityName", "City name already exists.");

        }
        if (ModelState.IsValid)
        {

            //Rest Of Code

            context.Entry(city).State = EntityState.Modified;

            return RedirectToAction("Index");
        }
        ViewBag.PossibleCountries = context.Countries.Where(f => f.IsActive == true && f.IsDeleted == false).ToList();
        return View(city);
    }
当我点击Save按钮而不做任何更改时,它也可以正常工作。它验证了我是否保存为“城市名称已存在”,我只想做的是:当用户想要用现有名称编辑和保存城市时,它不应该让用户继续。但是,当用户单击“保存”时,应允许用户继续操作,而不进行任何更改。 我正在使用ASP.NETMVC4和EF4。
提前感谢

有两种方法可以做到这一点,如果客户端使用jQuery没有任何更改,您可以阻止用户保存表单,或者您可以在控制器中使用下面的逻辑

控制器动作代码:

public ActionResult Edit(City city)
{
    var oldCity = context.Cities.Where(u => u.CityId == city.CityId).First();
    if (oldCity.CityName == city.CityName && oldCity.ContId == city.ContId)
    {
        // your code here 
        context.Entry(city).State = EntityState.Modified;
        context.SaveChanges();
        return RedirectToAction("Index");
    }
    // rest of your code
}
替代方法:

public ActionResult Edit(City city)
{
    var oldCity = context.Cities.Where(u => u.CityId == city.CityId).First();
    if (oldCity.CityName == city.CityName && oldCity.ContId == city.ContId)
    {
        // your code here 
        context.Entry(city).State = EntityState.Modified;
        context.SaveChanges();
        return RedirectToAction("Index");
    }
    // rest of your code
}
您可以在页面加载时序列化表单,然后在保存时将其与脏表单进行比较,然后相应地执行操作,如果表单的值有任何更改,则在控制器中调用您的操作,如果表单是干净的,则根据需要重定向用户


如果您不知道如何使用jQuery序列化表单。

谢谢大家,顺便说一句,我最后只做了一些更改并使用了嵌套if。这是密码

var CityCheck = context.Cities.Where(u => u.CityName == city.CityName && u.ContId == city.ContId).FirstOrDefault();        
if(CityCheck != null)
    {
         var CityCurrent = context.Cities.Where(t=> t.CityId == city.CityId && t.CityName == city.CityName).FirstOrDefault();
         if (CityCurrent != null)
         {
             return RedirectToAction("Index");
         }
         else
         {
             ModelState.AddModelError("CityName", "City name already exists.");
         }
     }
     else
         {
             context.Entry(city).State = EntityState.Modified;
         }
     if(ModelState.IsValid)
         {  
             context.Entry(city).State = EntityState.Modified;
             return RedirectToAction("Index");
         }
    ViewBag.PossibleCountries = context.Countries.Where(f => f.IsActive == true && f.IsDeleted == false).ToList();

    return View(city);

    }

编辑时,请在查看MVC时尝试使用displayfor helpers工具。它将帮助您获取mate标签中的详细信息,用户无法在运行时对其进行编辑。

从数据库中获取
City
,如果发布的值与现有值相同,则跳过验证。如果需要客户端验证,请使用
[Remote]
属性。