Solved

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

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

Expert Comment

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

Author Comment

by:yguyon28
Comment Utility
im using mvc 5
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now