Affan Shahab
Affan Shahab

Reputation: 888

How to validate duplicate Entries in EF4 while Editing

I have this code to check Duplicate Entries for Cities.

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

It Works Perfectly fine, Here is my Edit ActioneResult Method.

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

It Works fine too, While editing when I Click on Save button with out making any changes. It validates me to Save as "City Name Already Exist" All I want to do is that: It should not let user proceed when user wants to edit and save city with existing name. But should let the user proceed when he click on "SAVE" without making any changes. I am using ASP.NET MVC 4 and EF 4. Thanks In Advance

Upvotes: 2

Views: 520

Answers (3)

Aravindan
Aravindan

Reputation: 855

Try the displayfor helpers tool in MVC on view while on editing. it will Helps you to get the details in a lable for mate and users not able to edit them on them on the run time.

Upvotes: 0

Syed Farjad Zia Zaidi
Syed Farjad Zia Zaidi

Reputation: 3360

There are two ways for doing this, you can either stop user from saving the form if nothing has been changed on the client side using jQuery or you can use the logic below in your controller.

Controller Action 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
}

Alternative Way:

You can serialize the form on page load and then compare it to dirty form on save click and then perform the action accordingly, if the form has any changed value then call your action in controller and if the form is clean then redirect the user as you want.

Check this if you don't know how to serialize form using jQuery.

Upvotes: 0

Affan Shahab
Affan Shahab

Reputation: 888

Thank you every one, by the way I ended up with just a few changes and using nested if. Here is the code

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

    }

Upvotes: 1

Related Questions