Solved

MVC - Controller - Check If A Record Exists Before inserting - Call a method....

Posted on 2014-02-21
3
3,095 Views
Last Modified: 2014-03-03
I'm new to MVC and .net could someone show me how to write a controller that call a method to check if a record exist before calling a view on a post....
0
Comment
Question by:yguyon28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39878745
Which version of MVC are you using?
0
 

Author Comment

by:yguyon28
ID: 39878767
im using mvc 5
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39879363
If you are using Entity Framework to query the database you can build a query to get the records and then check to see if you have any results. For example lets say you have the Northwind database and are looking for a customer record.

// The customer you are looking for.
string customerToFind = "ALFKI";
// Query the database
using(var db = new NorthwindEntities())
{
	  // Find the customer you are looking for.
    var results = (from c in db.Customers
                   where c.CustomerID == customerToFind
                   select c).SingleOrDefault();

    // Test to see if the customer was found
    if( results != null )
    {
        // The record was found so do what you need here
        Console.WriteLine("You have the customer record that you are looking for.");
    }
    else
    {
        // The customer record was NOT found, Do whatever you need if the record was not found.
        Console.WriteLine("Customer NOT found");
    }
}

Open in new window

0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question