Configuration parameters in ASP.NET MVC - asp.net-mvc

Maybe I'm complicating this but you will enlight me for sure.
Imagine a page where a list is displayed. That list is paged.
I specify the list displays 30 items, therefore the controller returns my 30 items and the view renders the 30 items.
My question is: both the controller and the view need to know the 30 setting. Where should it be stored? Web.config? I'm thinking if sharing configs via web.config that are used across "layers" is a good practice...
Tks in advance

I don't think what you're describing is violating separation of concerns. By design data is shared across the layers of an MVC application, since the view has to get the data one way or another.
What you shouldn't do is have the view know how to get the data. In other words, your view shouldn't know where that "30" setting is coming from, so that you can change it in the future without changes necessary to the view.

Web.config is fine. Have you considered allowing the page length to be dynamic, perhaps based on a querystring value? (either way, you'll still need a default stored or hard-coded somewhere)
Also, I'm probably parsing words here, but you don't want your view to explicitly know of this setting (ie, accessing the web.config value directly). It should just iterate the list of items that the controller passes it, regardless of quantity.

Related

Does adding business logic in a view model in asp.net increase the size of the object being passed to the view?

This is something I've been trying to find definitive answers on and have found no luck online thus far. If in an ASP.NET project I have a view model that I'm using to pass data to the view in the browser, does including methods for accessing the model layer to fetch data increase the actual size of the resulting object being passed? My intuition is saying yes and I've been trying to read through other answers on stack overflow to find a solid answer to support it, but have had no luck.
The view model has some truly monolithic methods in it, some of them spanning 100+ lines or more going through LINQ operations to filter enumerable lists of data.
Nope, it shouldn't. A view is (typically) built from your Razor .cshtml and sent to the user. Only the contents of your view (the layout, bundled JS, stylesheets, page itself, etc) are sent to the user. The ViewModel is only used in building the view.
MVC has a concept called model binding, which is where incoming HTTP request's data is mapped to an appropriate set of parameters. On a request a new ViewModel will be created and passed to the appropriate Action method (or if the request can be mapped to a set of parameters, that also happens; doesn't have to be a single parameter).
On a side note, having a complicated ViewModel can make model binding hard. And having business logic in the ViewModel is also frowned upon; that makes it harder to reuse and follow. It's ideal to move it out into your business layer, or barring that into your controller.

Is there a way to pass value from one page to another in MVC

Is there any other way to pass value from one view to another or one controller to another apart from the methods below
Viewdata
TempData
Viewbag
or passing inline in page redirect code?
Any sort of communication between the views, excluding nested views i.e. partials, should go via the controller. That's kinda the whole point of MVC.
It sounds to me like you are sort of trying to get around the M/C part of the framework therefore it leads me to question is it the right tool for you to be using.

Maintaining input control state in MVC

In the app I'm working on the administrator has the ability to select a customer to work with in a drop down list. Thing is that there are a large number of views that the administrator can go through and each time they'd have to select the customer again. In webforms this was rather easy... store it in a Session variable and reset it when another page loads.
MVC not so much. I seem to be stuck at the point where I pass this value to the view from the controller. We are storing the value in a Session variable which we access using a base controller like this:
MyController.CurrentUser.CurrentCustomerId
The question I can't solve is how I pass this value to a partial view. The customer selector tool is in a partial view which is added to pages that need it.
I thought of using the ViewBag, but that means that in every single action in my controllers that requires this value I would have to add:
ViewBag.CurrentCustomerId = CurrentUser.CurrentCustomerId;
And even then I'm not sure if the ViewBag data is carried through to the partial view. I think it is.
Seems like there should be a more efficient way to do this and still abide by MVC rules?
#Andre Calil provided a very good solution for carrying user information between controllers and views and I ended up using this approach.
See his answer at the following link: Razor MVC, where to put global variables that's accessible across master page, partiview and view?

How do I design MVC apps with view auditing in mind?

We're going to be building an application using MVC 4. A key requirement is view-level auditing. In other words, we must be able to show who accessed a view/screen (person details, for example), the parameters to create the view and when the view was accessed. This is a new application so we can do almost whatever needs to be done to address this requirement.
The database will be SQL Server 2008. Data access will include EF 4.x, possibly 5.x but not a sure thing.
My question, how do I get started designing this feature? Are there any sample applications out there?
Don't mind auditing at view level. I think it is better to audit actions. You can do this using global action filters (for example here).
EF does not work very well with Views. You might be better of with either a straight ADO.NET persistence layer that you hand code, or Linq2Sql is a little better about using views (you still have to hook them up manually though). nHibernate would probably be the best ORM to use.
EDIT:
#user1469655 - You have a very fundamental misunderstanding of how MVC works. The "view" is not a page. It's a template. The "page" as you might consider it (ie accessing a specific url) is really a combination of two different things, a route and an action. An action can cause a specific template (view) to render, but there is not necessarily a 1:1 correlation between action and view.
An "action" is a method of a controller. This is mapped to a URL by a route. Typically (but not necessarily), this means something like this:
http://www.my.site/controller/action
When a user visits this URL, it causes the action method to be called, and potentially for a view to be rendered. Different actions can render the same view, because this is nothing more than an html template that can be rendered at will by any action method.
So what you want to do depends on what you are actually logging. It makes no sense whatsoever to log access to the view, because that doesn't actually mean anything. What you want to do is log access through the action, or possibly even log it through the route.
The simplest way to do this is to use a global action filter that is called before every action, in that filter you can log the information you need to (user id, date, time, action, referrer, whatever).. and it gets called for every action. It will not, however, get called for ignored routes (such as for content like javascript, images, css, files downloaded directly, etc..). If you need to log those, then you will need to control access to them via an action.
There's a good tutorial on Action Filters here
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs

ASP.MVC Permissioning : Is there a way to control partial content ?

I am writing an ASP.MVC application and I know you can use authorisation filters on the cotrollers to control access to the pages but I am wondering what is the best approach to do if you want to control access to protected data within partial views.
From what I have read ASP.MVC doesnt offer this level of granularity.
This seems easily enough by adding the user permissions to the model as an attribute and then using a simple factory to decide if the view should be rendered or a blank view be returned.
So far I have
RenderPartial(PartialFactory.IsAllowedToRender("partialName", Model.Security), Model)
and the Factory either returns the view requested or a blank partial view.
Has anyone tried this before or knows why no one does this (apart from the extra effort)
We have done similarly with extension methods for HtmlHelper.
RenderPartialIfExists and so on. No shame in doing this if its something you are going to need frequently.
Doing it without the extensions as you have works as well, but its not as clean to read. Also, consider adding your security information to HttpContext.Current.Items, that way you don't have to pass it into the models all the time, and anything that needs to take advantage of it, helpers, controllers, etc have easy access to it, and you only have to fetch it at the beginning of a request.

Resources