138
votes
13answers
7k views

Should sorting logic be placed in the model, the view, or the controller? [closed]

I have a drop down list that displays values from a table to the end user. I would like to have these values be sorted alphabetically. According to proper MVC design, at what layer should I place my ...
61
votes
4answers
13k views

Where does the “business logic layer” fit in to an MVC application?

First, before anyone screams dupe, I had a hard time summarizing it in a simple title. Another title might have been "What is the difference between a domain model and MVC model?" or "What is a ...
58
votes
14answers
7k views

ASP.NET MVC and Model-view-controller pattern - where do I start from? [duplicate]

Possible Duplicate: MVC Learning Resources I'd like to improve my understanding of MVC design pattern, as I'll be probably using it in the future (my job is mainly focused in developing ...
49
votes
4answers
13k views

Fat model / thin controller vs. Service layer [closed]

I have been developing enterprise applications for many years using .Net My apps usually have a domain model containing entities mapping to SQL DB tables. I use a Repository pattern, Dependency ...
47
votes
2answers
18k views

Using ChildActionOnly in MVC

I wonder why more objectively the use of attribute ChildActionOnly? I have a clear knowledge of the MVC as a whole, need to clarify the use of this attribute. Thank you!
35
votes
7answers
7k views

Better to have huge Controllers, or many controllers, in MVC?

We are building a fairly large HR application in ASP.NET MVC, and so far our controllers are becoming quite large. For example, we have an Employee controller, and all employee views are included ...
34
votes
3answers
11k views

ASP.NET MVC Controller Lifecycle

It's my understanding that the constructor for a controller is not called during each web request. Assuming this is true, what is the lifecycle of a controller? Is is "constructed" upon app start, ...
34
votes
3answers
11k views

How can I get the route name in controller in ASP.NET MVC?

ASP.NET MVC routes have names when mapped: routes.MapRoute( "Debug", // Route name -- how can I use this later???? "debug/{controller}/{action}/{id}", new { controller = "Home", action = "Index", ...
31
votes
3answers
32k views

How to pass parameters to a partial view in ASP.NET MVC?

Suppose that I have this partial view: Your name is <strong>@firstName @lastName</strong> which is accessible through a child only action like: [ChildActionOnly] public ActionResult ...
31
votes
8answers
6k views

How to structure an enterprise MVC app, and where does Business Logic go?

I am an MVC newbie. As far as I can tell: Controller: deals with routing requests View: deals with presentation of data Model: looks a whole lot like a Data Access layer Where does the Business ...
27
votes
4answers
4k views

How are you populating/validating your ViewModels?

I'm curious of all of the various ways people are building their ViewModels and why they choose that method. I can think of several ways here: -1. Injected repository - the controller loads the ...
26
votes
8answers
8k views

MVC Learning Resources [closed]

Can someone recommend some good resources for learning about MVC? I've been doing ASP.Net since it came out, but I've never gotten into MVC at all. I want to understand how it works, why it's better ...
24
votes
3answers
38k views

MVC Redirect to Index from another Controller

I have been looking through trying to find some way to redirect to an Index view from another controller. public ActionResult Index() { ApplicationController viewModel = new ...
23
votes
5answers
7k views

IE9 throws exceptions when loading scripts in iframe. Why?

Precondition: I have an aspx-page with iframe inside. This iframe points to the url handled by MVC on the same site (it's hybrid site, both standard ASP.NET and ASP.NET MVC). The resulting page ...
23
votes
5answers
5k views

Should a service layer return view models for an MVC application?

Say you have an ASP.NET MVC project and are using a service layer, such as in this contact manager tutorial on the asp.net site: ...
22
votes
3answers
26k views

How can dynamic breadcrumbs be achieved with ASP.net MVC?

How can dynamic breadcrumbs be achieved with ASP.net MVC? If you are curious about what breadcrumbs are: What are breadcrumbs? Well, if you have ever browsed an online store or read posts in a ...
21
votes
2answers
13k views

How can I check ModelState.IsValid from inside my Razor view

I have the following in my action method: if (!ModelState.IsValid) return View(vm); In the view I want to not present a submit key to allow deletion if the model state is not valid. ...
20
votes
5answers
8k views

Client Id for Property (ASP.Net MVC)

I'm a begginer in asp.net mvc, and I have a doubt: I'm trying to do a label for a TextBox in my View and I'd like to know, how can I take a Id that will be render in client to generete scripts... for ...
20
votes
3answers
3k views

“Security aware” action link?

How can I create a "security aware" action link that detects if a user is authorized to click (invoke) the action? Hide link if user is not allowed to use that action... Depending from web.config ...
20
votes
2answers
11k views

Is it possible, in MVC3, to have the same controller name in different areas?

In MVC3, I have the following areas: Mobile Sandbox Then i route maps like this: context.MapRoute( "Sandbox_default", "Sandbox/{controller}/{action}/{id}", ...
20
votes
1answer
21k views

C# ASP.NET MVC Return to Previous Page

I have a basic Edit method in my controller that redirects back to a top level listing (“Index”) when the edit succeeds. Standard behavior after MVC scaffolding. I am trying to change this Edit ...
19
votes
9answers
7k views

Where are the Business Rules in MVC

Now that everyone is talking about MVC, I notice that the business rules are not being addressed. In the old days of the 3-tier architecture, The business rules were in the middle layer. Where do they ...
19
votes
3answers
9k views

ASP.NET MVC Url.Action adds current route values to generated url

I have seen this question a couple of times here in SO but none of them with any acceptable answer: ASP.NET MVC @Url.Action includes current route data ASP.NET MVC implicitly adds route values ...
18
votes
5answers
774 views

How can I refactor my database access code outside my MVC project but keep my viewmodels inside?

I have an asp.net-mvc website with the following folders: Controllers Scripts Views ViewModels Models DomainModel I now want to access a lot of this business logic and database access code and ...
16
votes
1answer
6k views

How can I check if my model is valid from inside the razor view?

I need to do a check if my model is valid from inside my Razor view. If it's valid then I want to be able to show some HTML. How can I do this. I want something like @if ( Model.IsValid ) { } ...
16
votes
2answers
10k views

The ViewData item that has the key 'CategoryId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'?

So my code was working before. I don't know what I did for this to happen and I can't seem to fix it. I've seen people say to reset the ModelState. ( ModelState.Clear(); ) But that didn't help. Also, ...
15
votes
2answers
3k views

Extension methods cannot be dynamically dispatched

i want to have DropDownListFor in MVC @foreach (var item in Model) { @Html.DropDownListFor(modelItem => item.TitleIds, new SelectList(ViewBag.TitleNames as System.Collections.IEnumerable, ...
14
votes
4answers
38k views

How to create a pop-up window in asp.net mvc?

No javascript/AJAX to be used. when clicked on the hyperlink, it should open a new browser window.
14
votes
8answers
2k views

What Is ASP.Net MVC?

When I first heard about StackOverflow, and heard that it was being built in ASP.Net MVC, I was a little confused. I thought ASP.Net was always an example of an MVC architecture. You have the .aspx ...
14
votes
5answers
8k views

Querying Active Directory from MVC result in: Attempted to access an unloaded appdomain. (Exception from HRESULT: 0x80131014)

I have an issue using c# on .Net 4 in a MVC web application, where when I query Active Directory, I frequently get an error: Attempted to access an unloaded appdomain. (Exception from HRESULT: ...
13
votes
2answers
20k views

MVC [HttpPost/HttpGet] for Action

I am using MVC C#. Can somebody give an example on why one would use [HttpPost/HttpGet] for and Action. How can and active have both - what is the practical use?
13
votes
6answers
4k views

MVC Validation - Keep it DRY with a service layer - What is best practice?

I am trying to adhere to best multi-layer design practices, and don't want my MVC controller to interact with my DAL (or any IRepository for that matter). It must go through my business service layer ...
13
votes
5answers
3k views

Determine the model of a partial view from the controller within MVC

My current problem is that I have a partial view that I want to determine what model is being used by it. I have had to deal with a few strange scenarios for my project so I will try to outline it ...
13
votes
3answers
4k views

How to route a multiple language URL with a MVC

I need multi-language URL route of existing controller. Let me explain more: I have a controller with name "Product" and View with name "Software"; therefore, by default if the user enters ...
12
votes
7answers
760 views

How can I avoid tag soup with MVC?

I was reading this in my quest to justify MVC over non-mvc like regular old php (not using MVC, even classic asp could be used, albeit painfully): ...
12
votes
3answers
18k views

What is the difference between Html.Hidden and Html.HiddenFor

I can find a good definition for Html.HiddenFor on MSDN but the only thing I can find on Html.Hidden is related to problems it has. Can someone give me a good definition and an example.
12
votes
10answers
11k views

How do I maintain scroll position in MVC?

Im working on a project in MVC and have enjoyed learning about it. There are a few growing pains but once you figure them out it's not bad. One thing that is really simple in the WebForms world is ...
12
votes
9answers
5k views

Correct use of Model vs Controller in MVC / ASP.NET MVC

I have a Service class with a method called GetProducts(). That encapsulates business logic and calls the repository to get a list of products. My MVC view wants to show that list of products as an ...
12
votes
4answers
610 views

BestPractices: Is it acceptable to use more than one repository in a MVC-Controller?

I have a many-to-many assocition between an Employee and a Team. Foreach entity I have a repository. Now I use ASP.NET MVC and I created a EmployeeController. I also created a View to edit an ...
12
votes
2answers
4k views

MVC repository pattern design decision

I have an asp .net MVC application and recently started implementing the repository pattern with a service validation layer, much like this. I've been creating one repository/service for each model ...
12
votes
7answers
3k views

ASP.NET MVC - Partially updating model from view

I just wondered how people were approaching this situation. It's something that seems like a weak point in my usage of MVC with ORMs (NHibernate in this case)... Say you have a fine-grained and ...
12
votes
4answers
702 views

God Controllers - How to prevent them?

In a few MVC projects I've been working on, it has become apparent that there are a few problematic controllers that have organically grown into God classes - demi-gods each in their own domain, if ...
11
votes
4answers
11k views

asp.net MVC DisplayTemplates and EditorTemplate naming convention

I've got a couple of questions about the naming convention for the DisplayTemplates and EditorTemplates in MVC 2. If for example I have a customer object with a child list of account how do I: ...
11
votes
4answers
3k views

What type of architecture is this called?

For the web application (ASP.NET MVC) I'm currently developing, we have the following architecture in place: Data Access Layer: Logic for persisting data to an arbitrary db Domain: The data model ...
11
votes
3answers
10k views

ASP.NET MVC Html.ActionLink Maintains Route Values

I have a question that has pretty much been asked here: http://stackoverflow.com/questions/780643/asp-net-mvc-html-actionlink-keeping-route-value-i-dont-want However, the final solution is a kludge, ...
11
votes
2answers
3k views

How to get a ModelState key of an item in a list

Problem I have a list of fields that the user can edit. When the model is submitted I want to check if this items are valid. I can't use data notations because each field has a different validation ...
11
votes
2answers
6k views

ASP.NET MVC - Model binding a set of dynamically generated checkboxes - how to

I'm trying to model bind a set of dynamically generated checkboxes so as to process them in the controller action but can't get the model binding to occur. This is the scenario: My ViewModel class ...
11
votes
2answers
288 views

What is the elegant solution for unrelated views in MVC web frameworks?

I've had a problem with the following issue in Rails and ASP.Net MVC. Often there are multiple widgets of functionality on a page, yet one controller action is supposed to render the page. Let me ...
11
votes
6answers
2k views

Using MVC and fluent Nhibernate, how do I validate unique fields on my ViewModel before I bind them to my Domain Object and Save them?

I have a website where I allow users to create new Part records. I'm trying to figure out the best way to validate specific fields for uniqueness. I want to make sure that somebody doesn't try to ...
10
votes
4answers
2k views

Action Naming Convention

Has anybody established a good naming convention for action in MVC? I was specifically looking at ASP.net MVC but it is a general question. For instance I have an action which displays the login ...