【问题标题】:How to validate duplicate Entries in EF4 while Editing如何在编辑时验证 EF4 中的重复条目
【发布时间】:2015-02-07 00:59:27
【问题描述】:

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

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);
        }

效果很好,这是我的 Edit ActioneResult 方法。

    [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);
    }

它也可以正常工作,在编辑时单击保存按钮而不进行任何更改。它验证我保存为“城市名称已经存在”我想要做的就是:当用户想要编辑和保存具有现有名称的城市时,它不应该让用户继续。但是应该让用户在单击“保存”而不进行任何更改时继续。 我正在使用 ASP.NET MVC 4 和 EF 4。 提前致谢

【问题讨论】:

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


【解决方案1】:

谢谢大家,顺便说一句,我最终只做了一些更改并使用了嵌套 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);

    }

【讨论】:

    【解决方案2】:

    有两种方法可以做到这一点,如果客户端没有使用 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
    }
    

    替代方式:

    您可以在页面加载时序列化表单,然后在保存单击时将其与脏表单进行比较,然后执行相应的操作,如果表单有任何更改的值,则在控制器中调用您的操作,如果表单是干净的,则重定向用户随心所欲。

    Check this 如果你不知道如何使用 jQuery 序列化表单。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-07
        • 1970-01-01
        相关资源
        最近更新 更多