0

I want to write some sql queries(insert/update) in mvc3 .cshtm file and perform operations accordingly.I am new to this so I need help. Thank you in advance

2
  • 1
    Queries should be made in the controller, the result of the query can be passed with your model class or with dynamic ViewBag to your cshtml view. Commented Sep 10, 2013 at 10:43
  • "Queries should be made in the controller" - That's a fine place to start (compared to putting queries into Views), but its usually better to abstract those queries to a Data Access Layer (usually a different Class Library project), and to have your ViewModel call the Data Access methods. The Controllers are ideally "thin", and do not know how your Data Access is actually conducted. Commented Sep 10, 2013 at 12:06

2 Answers 2

1

You can do:

@{
   // You C# code here querying the database
}

BUT this is a very bad practice, such operations are the role of your Contoller.

In MVC your controller get and organize data and send data to the View. Then the role of the View is to only display data to the end-user.

If your data is complex you can (must) use a ViewModel (Views and ViewModels).

You can also use a ViewBag, ViewData, or TempData.

UPDATE:

using (var context = new BloggingContext())
{
    var blogs = context.Blogs.ToList();

    foreach (var item in blogs)
    {
        // Display blog for example
    }
}

If you are using EntityFramework you can find more details on their Home page

Sign up to request clarification or add additional context in comments.

3 Comments

Yes you are rt,but it is the reqiurement as I can make changes only in .cshtml file
In this case use @{ /*C# code here*/ } in your Views to query the database.
@would you pls give me an example?
0

The cshtml file is a view file in ASP.NET MVC. Accordingly, you would not put data access code within a view as it would be a violation of the design pattern; Views should be dumb and should be given the data they need to display a representation to the end user.

The controller should interact with the model to get data and pass it to the view. The general practice is to pass this data using a strongly typed ViewModel e.g.

public class MyController : Controller
{
    public ActionResult Index()
    {
        // your data access code here
        ViewModel model = someDataAccessCode.GetItemsForView();       

        return View(model);
    }
}

and then a view called Index.cshtml in ~/Views/My/

@model ViewModel

@* Display data in the view model *@

If you really wanted to put data access code in the view then you can use server-side script blocks to do so. For Razor views, these are

@{
    var data = someDataAccessCode.GetItemsForView();
}

@* Display data in the view model *@

But I wouldn't recommend this.

2 Comments

Thnx for ur reply,would u please give me any eg for insert operation?
There are many different ways to access data. I would start by looking at ADO.NET - csharp-station.com/Tutorial/AdoDotNet/Lesson01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.