Solved

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

Posted on 2014-02-21
3
2,832 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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