MVC logic in the view - asp.net-mvc

I'm creating an MVC5 web application and was curious if what I'm doing is considered bad practice or not.
I've already made a table with paging. This is created by a partial view that loops through the models id, code and name properties and displays them. Since I want to make a generic table I've decided to derive all models from a model base class (BusinessObjectModel) and give that to the partial view. I've modified the partial view to use reflection to get the values of every property this way the table can display every property of the model. This works great and the only thing left to do is to create a custom attribute tag to control which property is displayed in the list.
Is putting such logic into the view considered as bad practice? Should I create a helper that would assemble this view in the controller instead?

Related

Orchard Module as Partial view

I had created a module named Orchard.Blogs in my application. Now that blogs view can I make it as a partial view to my other views ? For Ex: http://localhost/Orchard.Web/Orchard.HrCompany/Payroll/GetPay is my url and in my GetPay View can I use Orchard.Blogs view as a partial view ?
In theory you can just use any view you like. However, many views require a view model. For instance, views returned by action methods probably have a view model. If you want to reuse that view, you will have to also provide an instance of that model.
Other views are used by shapes being rendered. If you want to render those views, you will have to actually create an instance of that shape. And if those views expect certain properties to exist on the shape, you will have to set those properties as well.
As you can probably see, this is not very straightforward, as it might potentially involve duplicating a lot of logic to build up the required data (think content part drivers that create shapes, for example).
What I would do instead, is let us know what particular views of the Blogs module you are looking to "reuse". Perhaps there are better ways. For example, there is a BlogArchives widget you might want to use. Or maybe a Projection Widget to render all posts of a particular Blog.
Depending on your exact use case scenario, there is bound to be a proper solution. Blindly reusing views from other modules is not typically done, because of the reasons I just listed.
All the views in orchard called "Shapes", and you can render any shape in any place with "IShapeFactory" interface, you can try the following code:
private readonly dynamic _shapeFactory;
public Constructor(IShapeFactory shapeFactory) {
_shapeFactory = shapeFactory;
}
public void CreateShape(){
dynamic customShape = _shapeFactory.CustomShape(ViewModel: viewModel);
}
This code with search on a view with name "CustomShape", then will render it using the named parameters we send.
On other side, you can display any shape directly in any view like:
#Display.CustomShape(ViewModel: viewModel)

How to use MVC Controllers for Layours in Sitecore?

I'm very new to sitecore and just started learning sitecore over at a dev-course held here in town.
The course is WebForms-focused, and since that's not my "patter of choice" I thought I would see how far I can get using MVC in Sitecore.
However, I have a slight problem. I have noticed how there are Controller Renderers, but and how they are bound to a Controller and such. but how about using a Controller for a Layout?
Lets say I have a layout, that for instance might statically render a menu on the top of the site. Then in this case I would like to avoid having a huge amount of code in my view for rendering the menu-items. Instead I would like to have build and populate a custom view model with the menu items and then simply pass down the model to the view and iterate through my menu items within my model.
But I just cant find a way of how to create a controller for a Layout. Any ideas?
Actually, this is possible, you can create a model deriving out of RenderingModel class or implement IRenderingModel interface and assign it to any MVC rendering or layout in Sitecore. Model object will be instantiated by the Sitecore's GetModel pipeline defined in Sitecore.Mvc.Config file.
See Here
Another way to handle this scenario is to create a Menu view rendering and then insert into a placeholder defined within the layout possibly a header placeholder. This can be statically assigned via standard values.
Kevin is right, thats not possible. Actually, Sitecore uses the layout to recognize a view extension and handle the request with MVC (TransferMvcLayout processor in the httpRequestBegin pipeline).
You should consider an approach where you add a placeholder in your layout:
#Html.Sitecore().Placeholder("menu-placeholder")
Then add a Menu Controller Rendering to that placeholder. That way you can utilize the rendering cache as well.
I don't think this is possible. But you can create a Razor view as layout, which works same as a View Rendering. Just just need to specify the path to the Razor view in the Path field and insert a model. In the model you could have simple property, which loads your menu items from whereever you want:
public class MyViewModel
{
public IEnumerable<MenuItem> MenuItems
{
return MyMenuUtil.LoadMenuItems();
}
}
This avoids lot of code in the Razor view and also in the model.

Sharing the HtmlHelper Class between a View and it's Partials

I'm have written a Custom Html Helper and and it can be called from any view like the following #Html.FootNoteNumberedLink. The problem is that I have a fairly complex set of partial views nested within the view and this particular helper needs to keep track of the data that has been added via other partial views. In other words, it is keeping a running tally of all the Footnotes for the entire view (that contains partial views). The problem is that #Html seems to be instantiated at every partial view load. Meaning that my helper can not have a footnotes 1,2,3 from partial view 1 and continue on with the footnote 4, 5, 6 on partial view 2, instead the HtmlHelper is new on every partial view hence the data can not be appended.
The internal workings of the footnote are using ViewData to store the list of foot notes as such: ViewData[ViewDataKey] as List
Any anyone explain why MVC is doing this or am I doing something wrong?
As usual thanks for your help.
The HtmlHelper is intented to be used as a simple markup generator class. Using it for keeping track of changes is probably not a good idea.
Instead, I would create a strongly typed viewmodel and populate it in the controller.
Then you can pass the viewmodel to your main view and just print it.
Your views should do nothing except painting whatever information the controller is passing.

MVC accessing model of view at layout page

I do not know the right approach to do it but I have one strange requirement in Razor
Scenario
I have a layout page being shared in 40 views, the layout pages also contain some label fields.
I have made a base class in Model which contains properties need to be displayed on Layout page, this class gets inherited in each and every model.
So basically when Model is passed to View it contain some properties to be displayed on layout.
Query
Is this is a right approach ? (If no then what is the right approach to do it?)
How to access the properties of model at view in layout page.
Partial View can be alternate but why not to make a Base class which contains Layout Properties and all the Model inherits Base Class
Check out this for more
Pass data to layout that are common to all pages

partial views in another controller

my problem is about returning a view from a different controller's views list. (MVC3)
first of all I am using areas, but in this case both controllers and views are in the same area.
in the controller;
in the DocumentController
I am returning a view from BelgeController like
return View("~/Areas/Fin/Views/Belge/Details.cshtml", belgeView);
the problem is in both view folders Document and Belge, there is a partial named edit.cshtml, and when Belge/Details view is rendered, Mvc finds and uses the wrong edit.cshtml, are there any easy ways of referring to the right partial view.
I used this approach all along the project, so there are 100s of Edit.cshtml's, so I am looking for an easy fix.
EDIT:
So, then the question is how can I pass a model to another controller with RedirectToAction.
I'm not sure on the use case, but generally speaking a controller shouldn't be serving anything that another controller is responsible for. it should either return a view of its won which has RenderAction calls to the other controller, or it should do a redirect.
You could just put the partial view in shared and give it a specific name, ie: BelgeDetails.cshtml
and then you should be able to return View("BelgeDetails", belgeView);
If you need to return a view form another controller, you should be using RedirectToAction. If you need a partial view, each controller should have its own sets of partial views that it uses for its own purposes. Controllers are meant to be self-service and self-reliant. Your pattern should not require different features from different controllers.
That said, it is perfectly reasonable to use jquery ajax calls to OTHER controllers to manage dynamic elements of your page (such as pop up modals and the like). This will allow you to create dynamic page elements without dumping all your code in one controller.
There is are two more options:
1) moving the data around in a tier in the model, so that the model of the BelgeController has the information needed, and call RedirectToAction.
2) hack it with ViewData passed to RedirectToAction.

Resources