4

I develop websites using web-forms, now I have a project where I am using MVC3 Framework with Rzor. My question is about some basic design patterns in MVC. I have a Webpage, where on left side i will Pull Categories from SQL Table, In Center I will Query another Sql Table, and few more all over the page.

So my question is...whats the best way to bring data into one webpage, all these queries are totally independant, do I need to create new MODEL for every Query? or there is a better way of doing it?

in WebForms I used user controls, where every user control had its own design & Sql Queries. I have heard about using Partial Views in MVC, but i am not sure, I guess i am having hard time understanding how to bring data into one webpage using different queries & show output on webpage.

Thanks

1
  • based on your comments to Gideon, you can certainly do that. But that's not what most people do. Most people use an ORM to get data, as it simplifies the whole process. But you're free to write any code as you see fit Commented Sep 4, 2012 at 5:12

1 Answer 1

8

You should create a ViewModel. Look at Update below

This is a model that represents your page. The elements you want to show in your view should exist in your ViewModel. You will populate the ViewModel in your controller and display them on the page.

I've written an example of a shopping site page, with categories on the left and Products in the centre. Both entities would exist in different tables.

Example:

class MainPageViewModel
{
  //this data is from a different table.
  //and goes on the left of the page
 public string Categories {get; set;}
  //this data is also from a different table.
  //and goes on the center of the page
 public List<Products> Products {get; set;}
}

In your controller:

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        MainPageViewModel vm = new MainPageViewModel();
        vm.Categories = GetCategories();
        //use the GetProducts() to get your products and add them.
        vm.Products.Add(...); 
        return View(vm); //pass it into the page
    }
    string[] GetCategories()
    {
     DataTable data = GetDataFromQuery("SELECT * FROM Categories WHERE..");
     //convert the data into a string[] and return it..
    }
    //maybe it has to return something else instead of string[]? 
    string[] GetProducts()
    {
     DataTable data = GetDataFromQuery("SELECT * FROM Products WHERE..");
     //convert the data into a string[] and return it..
    }
    DataTable GetDataFromQuery(string query)
    {
        SqlDataAdapter adap = 
             new SqlDataAdapter(query, "<your connection string>");
        DataTable data = new DataTable();
        adap.Fill(data);
        return data;
    }  
}

Then in your view you display it appropriately:

@model MainPageViewModel 

@{ ViewBag.Title = "MainPage"; }

<div id="left-bar">
  <ul>
    @foreach (var category in Model.Categories)
    {
        <li>@category</li>
    }
  </ul>
</div>
<div id="center-content">
    <ul>
    @foreach (var product in Model.Products)
    {
        <li>@product.Name</li>
        <li>@product.Price..</li>
        .....
    }
  </ul>  
</div>

Update

This is about your comment where you mentioned that your database tables and columns change regularly.

I can't say for sure but maybe you shouldn't be making tables like that everyday, maybe there is a better database design you could have, or maybe an RDBMS isn't the right thing for you and you should look into a NoSql database (like MongoDB )

Nevertheless if you continue with the above code I suggest putting this into a data layer class of its own.

Also take a look at Dapper it's a very thin data access layer that just gets objects from the database with sql queries or stored procedures. (Just exactly what you need) It's made and used by stackoverflow.

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

8 Comments

Thank you for your input and it does makes sense, however i wanted to ask whats the best way to get sql data into mvc. In webform i used to store data within datatable and bind datatable to controls. All the examples i see in mvc is to use linq to sql or ado.net entity framework, where i have to drag all tables into model and VS will create classes for me. Now say i just want to pull data simply like i do with web forms, a simple sql query and have a class return the data, how can this be done without doing and table drag or using wizards. I have around 100 tables . Thanks
In other words it would be great to provide a simple example and show me how to populate below by grabing a sql table.... vm.Catagories = //get your categories from
@highwingers you mean you want to execute your own sql with the SqlConnection, SqlDataAdapter, SqlCommand etc classes? What database do you have? Sql Server?
Yes. I have sql server and yes i would like to use system.data.sqlclient for all queries. Is it a good practice? Ty
Perfect, i will try it and will let you know. Since i am new to mvc, could you advise if this process of getting data is a normal way of doing this or entiry framework is highly recommended. ?
|

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.