2

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

Affan Shahab
  • 838
  • 3
  • 17
  • 39
  • 2
    Get the `City` from the database, then if the posted value is the same as the existing value, skip the validation. If you want client side validation, use the `[Remote]` attribute. [How to: Implement Remote Validation in ASP.NET MVC](http://msdn.microsoft.com/en-us/library/gg508808(VS.98).aspx) –  Dec 09 '14 at 07:52

3 Answers3

1

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

    }
Shah Abdullah
  • 405
  • 2
  • 16
Affan Shahab
  • 838
  • 3
  • 17
  • 39
0

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.

Community
  • 1
  • 1
Syed Farjad Zia Zaidi
  • 3,302
  • 4
  • 27
  • 50
0

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.

Aravindan
  • 855
  • 6
  • 15