Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6575299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:22:12+00:00 2026-05-25T15:22:12+00:00

Hi i’m new to MVC and EF so this may be a really simple

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:22:12+00:00Added an answer on May 25, 2026 at 3:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.