Validation in MVC Entity Framework to avoid duplicate records

asp.net-mvc entity-framework validation

Question

Hi i'm new to MVC and EF so this may be a really simple question but what is the best way to prevent the user from trying to enter duplicate records?

I have a simple look up table with one column which is the primary key. I'm creating a maintenance screen so admins can add extra items to the look up list. My controller looks like :

public ActionResult Index(string NewRow)
    {
        try
        {
            CaseType t = new CaseType { ID = NewRow };
            if (ModelState.IsValid)
            {
                UOW.CaseTypes.Add(t);
                UOW.Save();
            }
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("", ex.Message);
        }
        return View(UOW.CaseTypes.All());
    }

Which stops the duplicate records and stops the yellow screen of death but the message displayed is : "An error occurred while updating the entries. See the inner exception for details." which is no good for the users.

I was just wondering if there is a better way to catch the error.

1
2
7/5/2013 10:07:28 AM

Accepted Answer

For show validation error I use something like this:

MainEmail it's property from ViewModel

        var mod = ModelState.First(c => c.Key == "MainEmail");  // this
        mod.Value.Errors.Add("Row's shouldn't duplicates.");    // this

        if (ModelState.IsValid)
        {
            return RedirectToAction("Details");
        }

        return View(client);

Error will show's in this field in view:

    <div class="editor-label">
        @Html.LabelFor(model => model.MainEmail)
    </div>

And for future, you must hide your error screen! You need to display a custom error page:

If you use asp-mvc-3, add to web.config such string:

  <system.web>
     <customErrors mode="On" defaultRedirect="~/Error" />
     ...

And users will have /Shared/Error.cshtml page insted of page with exception message (which can show sequrity data).

ADD

About unique constraint creation was discussed here Unique Constraint in Entity Framework Code First

than you can check about records duplication with your try code.

In my application I use code first and don't add unique constraint because it's lead to low testability. In that case use simple Find before saving changes.

First aproach is little bit faster.

Approach two is grow up testability.

4
5/23/2017 12:08:26 PM

Popular Answer

If you want to get the inner exception you can do like this,

catch (Exception ex)
        {
           while(ex.InnerException!=null){
                 ex=ex.InnerException;
           }
            ModelState.AddModelError("", ex.Message);
        }

no too sure about the syntax :)



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow