When to use Custom HTML Helpers Asp.net MVC - c#

Simple question;
When are Custom HTML Helpers supposed to be used. Are we supposed (as intended by the developers of MVC) to implement an Extention method for all tags/logic that are reused across the views?
I'm curious about this, as we currently have a medium sized project that needs to be refactored. Alot of different interns have been working on this, and its time to do a cleanup.

Use Html Helper for rendering an Html Element. That element may contain inner elements, but your helper should mean the outermost element. Let me give you an example. Say, you want to render an Image button with some specific attributes so that your image button will look like this:
<button title="my button" onclick="onClickFunctionName()">
<img src="...." />
</button>
So, an Html Helper with following signature can be very useful:
#Html.ImageButton(title:"my button", onclickFunctionName:"onClickFunctionName", imageSource:"....")
Same like an image button, you can encapsulate creating a Table using Html Helper.
If you want to encapsulate a piece of html markup for some partial layout, then you should use Partial Views. For example, a Partial View can be used for rendering Login box with User name and Password and a Remember Me Checkbox. In this case, an HTML Helper wont be a good idea to use.

In situations where you want your links to work with your routing configurations. You use HTML property. It's instance of IHtmlHelper which contains methods for generating snippets of html. You could type them by hand, but this html property has a logic behind the scenes to use some metadata or configurations in your application. Also, HTML Helpers know built-in conventions in the MVC framework.

Related

Prefered way of building multi view layouts in ASP.NET MVC?

I'm currently wondering about the prefered way to build multi view layouts using ASP.NET MVC.
For example in angular-based SPAs, loading different views into a layout is pretty straightforward:
<div ui-view="header"></div>
<div ui-view="content"></div>
.state("home", {
views: {
"header": { templateUrl: "app/_header.html" }
...
However in MVC, there seem to be multiple ways to achieve something, but I'm extremely confused about what 'method' is originally intended for what purpose.
#Html.RenderPartial() - renders a 'partial', defined by the parent
#Html.Partial() - seems to be the same as above
#RenderPage() - renders a 'page', defined by the parent
#RenderSection() - renders a 'section' by id, section defined by 'body view'
For a common "header, body, footer, nav" layout, what is the prefered (or the right) method? Or is it just a question of personal preference?
In your scenario, I would go for #Html.Partial just in case you need to manipulate the HTML inside your SPA (as you can assign it to a variable).
As for common elements, use RenderPartial, this can be slightly faster (albeit nothing to write home about).
Here's the explanations:
Html.RenderPartial
Renders the view directly to the response stream, similarly to how a normal web page is served by the server.
Html.Partial
Renders the view as a string, if you want to assign the result into a variable and manipulate it, then use this.
RenderPage
Same as RenderPartial, but allows you to specify a path to the view should you need to.
RenderSection
Usually used in layouts to define sections that will be included within child pages, this is especially useful if you want scripts/styles included in the head tag that you wouldn't be able to dictate within a child view.
There's also:
RenderAction
This takes a controller and action and will render the result of that action, useful if said view requires a different model and the data to populate that model is stored in a common function/library.

Generating Form Elements in code

I have a web app that has a form on just about every page. In order to make sure each form renders the same, as to make changes easier, I want to render all of my form controls in c# in a central place so if I need to add a class to the input or change something, I only have to do it once.
At the moment, I am just using a load of static classes like TextInputHelper, CheckboxInputHelper etc that use StringBuilder to build up the HTML and returns a string to my view.
For example, all of my forms controls are of the basic form:
<section>
<label class="label">Label Text</label>
<label class="input">
...Input Element...
</label>
</section>
What I would like to do is improve this situation as I still have a lot of duplication between the different helper classes, particularly for the wrappers to the form elements. My initial thoughts are to have a class called something like BaseFormControl that has a virtual Render method that has the outer wrapper for the control, then create other classes that implement this to do specific things like a TextFormControl that puts
<input type="text".....
inside the wrapper.
Am I on the right path for this, and/or is there a design pattern that is appropriate for what I want to do?
From my POV what I read is that you over complicating your server side just because of a CSS/HTML standardization that be easily solved from the client side.
If you still want to go for the server side approach the pattern that seems to apply to your approach is the Adapter(Wrapper) pattern meaning that you probably would want to create control wrapper clases to meet your needs for each control that renders a different html from what is offered by ASP.NET built-in controls.
If what you want to do instead is to format a group of controls then a UserControl is way much better approach.
Remember that ASP.NET was build with the spirit of trying to keep layers separated the View from the Code therefore any attempt to generate html from the server side has to be an exception and not a rule.

MVC 4 Razor user control

I want to create something like a user control in my MVC 4 project. I have followed the instructions in this article so i have added the App_Code folder(it was not initially there) in the project. Inside i have put a file
VDRazorHelpers.cshtml
Inside that file i have my code which is actually a static html table(i would add dynamic values later):
#helper PropertySummary(){
<tr>
<td width="210" align="center" colspan="2">
<img src="http://www.mysite.com/images/BG1.JPG" width="210" height="140" />
</td>
</tr>
}
Then in my view i try to access that helper writing:
#VDRazorHelpers.PropertySummary
but this doesn't seem to be available.
I know the article i mentioned is for MVC3. Has it change? What would be the proper way of having custom reusable html parts in MVC4?
Here is the screenshot of the intellisense:
I would be more inclined to use a partial view which you could place in the shared views folder then call passing the model you wish to render with that view:
#Html.Partial("propertySummary", property)
Just a thought
Partial Views are the closest to an analogue of a Web Forms User Control in MVC. They encapsulate HTML that can be reused on other views or returned directly from action methods, which is handy for AJAX calls that need to fetch some HTML. Big difference in MVC is that the Partial Views don't have the ViewState of a User Control in Web Forms, nor does anything, it's gone, and good riddance as far as I'm concerned. HTML Helpers could also be considered close to User Controls however they are completely based in code and generate HTML whereas Partial Views are the markup.

Can I populate a ContentPlaceHolder in a master page from within a Razor Partial View?

I'm using the typical built in view engine in mvc3 (is there a proper name for it?) for views and master pages and it's including a Razor partial view on the .aspx page. In the masterpage, there is a ContentPlaceHolder with an ID of "ScriptContent".
I want to be able to fill that ContentPlaceHolder from within the Razor partial view but I don't think this is possible. Does anyone know if it is possible and how I would go about doing that?
I already tried rendering it in the partial like so, but that didn't work.
#section ScriptContent {
... content ...
}
It would be very difficult, so much so that I'd recommend finding another way :(. I wish it was easier, but these are the complexities of integrating a new view engine into an existing legacy system.
To give you a head start if you really want to try it: You'd probably need to create a custom base class inheriting from WebViewPage for your Razor content pages, override some of the methods (honestly I'm not too familiar with that aspect so you'd need to debug to follow the pipeline) so that instead of treating the Layout property as the path to a Layout page, you treat it as a Master page. Then you'd need to instantiate the master page and somehow convert the Sections (which were transformed into calls to DefineSection by the Razor parser, and should be stored in a Dictionary somewhere on the base class) in to Content controls and stuff them in the Master Page.
If I haven't boggled your mind by this point, you may just be able to pull this off, but to be honest, I'd avoid it.
P.S. We refer to the older view engine as "ASPX", based on its file extension ;).

MVC3 Layout Page, View, RenderPartial and getting script files into the Header (from the partial view)

So I have a Layout page
<head>
#RenderSection("HeaderLast", required: false)
</head>
A view
#section HeaderLast
{
<script src="#Url.Content("~/Scripts/knockout-1.2.0.js")"
type="text/javascript"></script>
}
<div id="profile-tab">
#{ Html.RenderPartial("_userProfile"); }
</div>
And a Partial view
#section HeaderLast
{
<script type="text/javascript">
alert('test');
</script>
}
<div......
I figured it couldn't be that simple. Is there a proper way to do this out of box or will this always require some kind of mediator and passing stuff around ViewData to manually make the content bubble up to the layout page?
Bounty started: The bounty will be rewarded to the best solution provided for this short coming. Should no answers be provided I will award it to #SLaks for originally answering this question.
You cannot define sections in partial views.
Instead, you can put the Javascript in ViewBag, then emit any Javascript found in ViewBag in the layout page.
#JasCav: If a partial needs its own CSS, it has no good way to get it rendered.
If that's the reason for its use, it could very well be by design.
You don't want to have a separate CSS file x partial/helper. Remember, each separate CSS file means a separate request to get it from the server, thus an additional round-trip that affects time to render your page.
Also you don't want to emit direct CSS to the HTML from the partial/helper. Instead you want it to have appropriate hooks you can use to define all the look in your site's CSS file.
You can use the same hooks you have available for CSS to activate custom JavaScript behaviors for the elements involved When JavaScript is enabled.
Finally it may be the case what you need is not a Partial View, but an extra Layout you use for some pages. With that approach you would have:
A master Layout that gets set automatically on _ViewStart like you probably has now. This defines the sections like in your sample.
A children Layout page. Here you have both the extra html, css, js you need to have for these views. This uses both #RenderBody() and #section SomeSection { } to structure your common extra layout.
Some views that point to the children layout, and others that use the default master layout.
How to get extra data to the children Layout is out of the scope of the question, but you have several options. Like having a common base for your entities; using ViewBag or calling Html.RenderAction to get that shared logic related to shared dynamic elements in the layout.
It looks like there was a similar question on SO - How to render JavaScript into MasterLayout section from partial view?.
Unfortunately, there is no possibility of declaring sections inside Partial Views. That is because RenderPartial ends up rendering totally separate view page. There is a workaround to this, though a bit ugly. But it can look better if using strongly-typed model instead of ViewData.
Basically, you need to keep track of the reference to the view which called RenderPartial and use the DefineSection method on the object passed to push data to that view.
UPDATE: There is also a blog post about dealing with RenderSection you may find useful.
Here is another approach using helper methods and templated delegate
http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor-nested-layouts-and-redefined-sections.aspx
As a follow up to my question, the JavaScript/CSS combiner/minifier tool Cassette supports this functionality to allow you to compartmentalize your JavaScript and other assets that are required for partials.
I purchased a site license and use this in all of my MVC applications now.

Categories

Resources