Where do methods of an object go in MVC?

SO, this is what I have:

I have a model which stores the lat and lng of a geographical location, call it location with its own database and table.

public class location { public int Id { get; set; } public int lat { get; set; } public int lng { get; set; } }

and I want a method in the line of:

function getDistance(location l){ return Math.sqrt(l.lat - this.lat).... etc etc. }

Where should this go? Probably not model? Does it belong to control? Since it's pretty universal, should make a control that is not associated with a view?

If you could suggest some reading, that would be nice too....

Thanks for the advice(s) in advance!


Firstly, do what you want, it's your code, make your own mistakes, think about why things aren't working or are working for your problem domain.

But, typically.

If your object is a View then a common practice is to use a POCO (Plain old C#/CLR Object), that is a class that only has properties that you want to bind with your display. You can have as many views of the same model(s) as you like depending on your situation. But they don't normally include any logic.


The Model is where all your business logic goes. The thing with the frameworks people are using is that it encourages the building of CRUD applications which essentially are just an access database on the web. Your Model is not just the entity map to your database. Your Model may use some form of persistence but it isn't necessarily the only activity it performs.

The Controller coordinates action between the client and the Model. In MVC an operation might ask the Controller for another page of data. It's the Controller's responsibility the ask the Model (or a cache, security framework or anything else) to return the required information it needs to compose the View (the answer) to the question posed to the controller.

Putting it in the model would be just fine.

public class location
public int Id { get; set; }
public int lat { get; set; }
public int lng { get; set; }

public double? getDistance(location l)
if (l != null)
return Math.Sqrt(l.lat - this.lat).... etc etc.
return null;

This will return the distance from one instance of location to another.

I know it isn't part of your question, but I think it's important to mention: it's pretty standard convention to capitalize the first letter of a class and method definition.

Category:asp.net mvc Time:2018-08-28 Views:1
Tags: asp.net mvc

Related post

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.433 (s). 13 q(s)