formatc
formatc

Reputation: 4323

Beginner mvc where to call methods

I am doing big project in Web Forms and entered a big mess of code(can be solved with little refactoring), so I decided to take a peek at mvc, seems like everyone favores it over web forms.

I have dll file with all LINQ to SQL classes and methods for existing sql db and I started teaching myself by reacreating this project in mvc.I first recreated my homepage from webforms to razor, so far so good and I recreated one of the user controls to partial view and looped it. Partial view is strongly typed with one of L2S class, the thing is there is some raw data in it.Like for example My model for partial view is my L2S class PostDetails: it consist od some data ready from output like: Title, Permalink, ViewsNumber etc. but it also cointains some raw data like UserId(I need to call method from dll to get username),DateTimeCreated(on which I also need to call method to get time difference), In webforms I would do this in codebehind but I'm not sure where to do it in mvc, maybe in controller and pass it in ViewData.I am also asking this for future, not just this case.

Upvotes: 0

Views: 314

Answers (3)

Justin Pihony
Justin Pihony

Reputation: 67075

You should perform those actions in the controller. The controller is exactly what it sounds like, it controls the data flow between the model and the view.

Here is an example using your PostDetails:

PostDetailsModel

String Title {get;set;}
String Permalink {get;set;}
Int ViewNumber {get;set}
Int UserId {get;set}
DateTime DateTimeCreated {get;set;}

GetDetailsView: this will be requested by your user, and will be a visual representation of the PostDetailsModel (however you want to format that). When this View is requested, a call is made to your controller....

PostDetailsController

//This method will (by default) come from calling [BASEURL]/PostDetails/GetDetails
public ActionResult GetDetails()
{
    var details = new PostDetailsModel();
    details.UserId = GetUserId();
    details.ViewNumber = GetViewNumber();
    ....
    //By default this looks for a view in the PostDetails folder 
    //by the name of your method (GetDetails)
    return View(details);
}

Notice how the controller is the router between the model and the view, basically. A note, however, it would be better to fill your model from methods contained within some sort of business layer (however you implement that). Something like var details = BL.GetDetails();

Also, when the user makes requests to save data, then you can do that with another method that takes the data (whether it be the PostDetailsModel or an int or...) and does whatever it needs to do, then it can redirect back to the display action (or wherever you need it to go)

There is a wealth of information on MVC with a simple google search. Here is Microsoft's overview, but the wikipedia article is very succinct if you just want the basics

Upvotes: 1

Neil Knight
Neil Knight

Reputation: 48547

I think that you'll find this article very useful:

MVC Overview

It explains in detail, as to what each component should be used for:

Models. Model objects are the parts of the application that implement the logic for the application s data domain. Often, model objects retrieve and store model state in a database. For example, a Product object might retrieve information from a database, operate on it, and then write updated information back to a Products table in SQL Server.

Views. Views are the components that display the application s user interface (UI). Typically, this UI is created from the model data. An example would be an edit view of a Products table that displays text boxes, drop-down lists, and check boxes based on the current state of a Products object.

Controllers. Controllers are the components that handle user interaction, work with the model, and ultimately select a view to render that displays UI. In an MVC application, the view only displays information; the controller handles and responds to user input and interaction. For example, the controller handles query-string values, and passes these values to the model, which in turn queries the database by using the values.

Upvotes: 0

Shyju
Shyju

Reputation: 218732

In MVC, All your requests will be handled by an action method in a controller. and then controller returns a view. So you can get the data in your controller (or a different layer which will be called from the controller) and pass that data to your view.

Strongly typed views are a clean way of doing things. Create a ViewModel for your scenario. Mostly ViewModels looks similar to the Entities. Ex : For Displaying the details about a customer, i will create a viewModel called "CustomerViewModel"

public class CustomerViewModel
{
  public string CustomerId { set;get;}
  public string FirstName  { set;get;}
}

Then in my CustomerController, I will have a get method for the customer

public class CustomersController : Controller
{
  public ActionResult Details(int id)
  {
    CustomerViewModel objCustomer=new CustomerViewModel;
    objCustomer.FirstName="Samuel";
    //Instead of hardcoding the values here , you can get it
   // from the database using the id and assign to the properties
    return View(objCustomer);
  }


}

And you will have a view called "Details.chtml" in your Views\Customer folder which is strongly typed to your CustomerViewModel

@model CustomerViewModel

<div>
   <h2>@Model.FirstName</h2>
   <p>@Model.CustomerId</h2>
</div>

This can be accessed like http://yourdomain.com/Customer/Details/25

Personally i prefer to keep my controller actions thin. so i write the GetFromDatabase code in a seperate service layer and i just call that method from my action method

Upvotes: 1

Related Questions