HTML Helpers and Partial Views - asp.net-mvc

If I have say a Partial View called MypartialView and I have a HTML Helper called "MyHTMLHelper" how can I return a partial view from the helper?
My requirement is that sometimes I'd like to render a PartialView on it's own and other times I'd like to render it with another partial view, or a slab of text or something.
So I thought I could create a helper that return both partial views, and a html helper that would return the partial view along with a slab of text.
is this best practice or should i instead create a partial view that has both partials in it and another that has a the partial view + the slab of text?
I'm not only looking for source but also the best practice according to what people are doing.
thanks.

I would use two Views:
-With 2 to partials
<% Html.RenderPartial("Partial1"); %>
<% Html.RenderPartial("Partial2"); %>
-The Partial and some text
Some Text
<% Html.RenderPartial("Partial1"); %>
I think the concept of DRY is still there, because at the end you still have all the code in one place, the Partial Views, and you just reference it from another two Views.
Doing it the other way will be complicated, and I don't think its really necessary to use another Helper Method to accomplish this.

Helpers seem to be designed to be reused a lot more heavily than partials so i'd suggest that if you think you will use the rendered result from the helper as much as you would with the alternative method (nested PV) then go with the helper.

Related

Include variable definitions in multiple views

In a number of views I have the following kind of code:
<% compute a number of variables %>
Do something with those variables to layout and configure HTML elements.
I'm drying to dry up my views. That first part where I compute the values for the variables is used in a number of different views. I would like to be able to pull it out into another file and use it in each view that needs it so that I only have to change it in one place. If I pull it into its own partial and then render that partial in another view, the variables are not defined in the view. To be more specific:
<%
top = 0
link.siblings_before.each do |sibling|
top = top + compute_top(sibling)
end
%>
<div style="position:absolute;top:<%= top %>px"><%= link.name %></div>
I want to be able to use the computation of top in more than one view without having to cut and paste the ruby code into each view.
Try avoiding logic in your views.
You can define a before_filter in your controller and enabled it for multiple actions that you need them for.
Those variables defined in your before filter will be available in those views and you keep your views clear at the same time.

Rendering two views at the same time, or a partial and a view

I'm working on a site right now where I have a standard layout, with a view/template. I've spent the past 2 weeks trying to get ajax functionality working, but it's not working and I need an alternative. Is it possible to render a partial WITHIN a view?
As in, layout (view (partial)) sort of thing?
Right now I just have a standard layout and view with a yeild in the layout. There is an empty div in the view where the ajax HTML was supposed to go, I want to fill it with a partial for now
Yes surely, you can call a partial inside any view file. For example, you have a view file 'index.html.erb' and you want to call a partial '_new_partial.html.erb'
In your view file.
<div>
<%= render :partial => 'new_partial' %> #provide complete path if partial is not in the same directory.
</div>

Rails: Nested views or partials or..?

I new to Ruby on Rails, and I am coming from Drupal/PHP background.
I am trying to do something simple. A "Static" controller for some pages like about us, disclaimer etc. and a "Video" controller for videos.
The whole application has same look. I am trying to figure out how I can avoid duplicating views.
In drupal the theming goes like this:
html.tpl.php (renders page.tpl.php)
- page.tpl.php (renders multiple block.tpl.php)
- block.tpl.php (renders more nested tpls or some html generated by theme functions).
I find this to be great.
In rails, what I read is there is application.html.erb (Which I think is similar to html.tpl.php of Drupal, as it normally contains stylesheets, js, etc. available throughout the application) called "Layout". Then there are "Views", which "yield" content in this layout. We specify the view in controller, or it gets one automatically. I think of a view as "page.tpl.php" of Drupal.
Most of my site uses same structure i.e. header, top menu, content, right sidebar, footer. But content of each area changes depending on the path. So I am thinking of doing it like this:
Both 'Static' & 'Video' will call the same view, with different variable values i.e. values for content area and sidebar.
This View will the use 'render' to render another view, with a subset of variables passed from controller i.e. it will render content & sidebar views.
If needed, those views will render more views, and so on.
Another one I read about is partials. I can't seem to differentiate between rendering a view inside another view, and rendering a partial.
Which method is better? Or is there a better "Rails Way"?
This is quite a large topic to cover but edited highlights are
If you want to repeat content on every page, like a header or navbar for example then put it in layouts/application.html.erb
if you want to keep your views tidy with content specific to that page then you can use partials. Normally in views I create a folder called shared and then put my partials in there. for example if i save a partial called _form.html.erb (make sure you name partials with the underscore at the beginning of the file name), i would then call that in my view like
<%= render 'shared/form' %>
you can also use partials in the layouts/application file to keep that clean aswell
This is just my way of doing it, I'm sure more experienced rails guys have better ways
Hope that helps, any more questions then just ask

What is the difference between a View and a PartialView in ASP.NET MVC?

What is the difference between a View and a PartialView in ASP.NET MVC?
At first glance the need for both seems non-obvious to me.
In theory, the answer is: A partial view is a "sub-view" that you embed within a main view - something that you might reuse across multiple views, like a sidebar.
In practice, the answer is: Very little.
In theory, partial views are more lightweight than standard views, but it's perfectly OK to pass a "regular" view to RenderPartial and the performance seems to be exactly the same. I frequently use regular .aspx views as "partial" views because you can make them reference a master view in order to provide templated content like what you can do with UserControls in ASP.NET WebForms. See here.
Partial views are more like web parts on a portal - they are completely self-contained objects. Use them if the layout is simple and static, or if you're annoyed by the Intellisense errors when you don't have the <html> and <body> tags in a standard View.
It works like that:
return View() the view content goes in the #RenderBody() of the /Shared/_Layout.cshtml
return PartialView() it returns only the view content
Views are the general result of a page that results in a display. It's the highest level container except the masterpage. While a partial view is for a small piece of content that may be reused on different pages, or multiple times in a page.
If you're coming from webforms, view is similar to a web content form, while a partial view is like a user control.
If you come from a webforms background, think of PartialView as a usercontrol.
Look at StackOverflow.com site:
Main site (View) contains components like:
Tags
Related
Ad
So Tags, related, Ad etc. can be composed as PartialViews. The advantage of this is that
PartialViews can be simply cached by OutputCache instead of recreating all site: performance gain.
Consider a partialview like a control in webforms, the idea is the partial is reusable
View in Core Mvc
View Contains The layout Page(_ViewStart) Or Layout Property.
#{
Layout="_Layout"; }
Before Any View Rendered _ViewStart is Rendered.
View might have markup tags As
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
View isn't Lightwight as Partial View.
View will Render in Layout by used #RenderBody() Method
(Views/Shared/_Layout.cshtml).
Calling by use return View() inside Action.
Partial View
Partial view doesn't Contain Layout Page Layout Property
It does not verify for a viewstart.cshtml. We cannot put common code
for a partial view within the viewStart.cshtml.page.
In MVC Partial view is designed specially to render within the view
We can pass a regular view to the RenderPartial method.
#{await Html.PartialAsync("Partial"); }
Example To Demo Partial View Is Strongly Type
in View
#model List<Student>;
#{
Layout = "_Layout";
}
<div>#await Html.PartialAsync("Partial",Model)</div>
in Partial View
#model List<Student>; <ul>
#foreach (Student student in Model)
{
<li>#student.ID</li>
<li>#student.Name</li>
<li>#student.Age</li>
}
</ul>
SP.NET Core MVC looks for Partial View in the same way like Regular
Views (i.e. in Views/ and Views/Shared folders). This
means you can create controller specific partial views or shared
partial views.
Good Luck

ViewData not inheriting in partials

I was trying to use a shared partial view to render when a particular listing page has no data. I wanted to use ViewData to pass information from the page into my listing control, which would then conditionally render the NoData partial view using the ViewData values.
I would like to be able to specify them in the view markup, not in the controller action, but when I add them in the view the don't seem to inherit down into child partial views (like the Nodata partial view). However, specifying them in the ViewData values in the controller actions works fine, the data is available all the way down...
Does anyone know why it behaves this way?
When rendering a partial you can also pass the ViewData.
<% Html.RenderPartial("NoData", ViewData); %>
<%Html.RenderPartial("partialViewName", "viewData", "model"); %>
it is a best practice to do the decision inside the controller, if you have a scenario to make a decision inside the view, separate them and call them inside the controller conditionally

Resources