partial view rendering - asp.net-mvc

I have a partial view in my Foo folder. I want to show it on my Home/index view. I am using partial render and it is trying to locate it in temp folder. How to write Renderpartial to render foo\partial view ?
regards,
Asif hameed

To get it to render just specify the path in RenderPartial like this:
<%Html.RenderPartial("~/Areas/FooArea/Views/Foo.ascx");%>
Obviously replace my example path with the path to your actual Foo partial view.

If you want to call an action that returns a partial view on another controller (foo) try using Html.RenderAction. It will allow you to pass in an action and a controller.
This post has a decent description of the differences between RenderPartial/RenderAction and when to use each one: http://www.arrangeactassert.com/when-to-use-html-renderpartial-and-html-renderaction-in-asp-net-mvc-razor-views/

Related

Html.RenderPartial Throws System.InvalidException while trying to render a partial view

I am trying to render a partial view using RenderPartial, it keep throwing System.InvalidOperationException. Below is the folder structure that shows the Views folder.
I am accessing Simulation from Index and have tried below combination the error is same for all.
#{Html.RenderPartial("/Shared/Simulation");}
#{Html.RenderPartial("/Views/Shared/Simulation");}
#{Html.RenderPartial("~/Views/Shared/Simulation");}
I know it is some thing silly i am missing here
When using the full path, you need to include the file extension as well.
This should work.
#{ Html.RenderPartial("~/Views/Shared/Simulation.cshtml"); }
Or you can simply not use the full path, but just pass the view name
#{ Html.RenderPartial("Simulation"); }
Razor view engine will look in the ~/Views/Shared directory and render the view as the location follows the convention.

Controller action for partials

I was trying to create an action for one of the partials in my rails application, but wondered how to name it. Does it have the underscore at the beginning? Like:
def _my_partial
end
or no underscore? Not mentioned in the controllers guide
A partial is a piece of view which is embedded in the main view or within another partial. The purpose of a partial is typically for re-use or to making view code cleaner.
If you need to use an action to render a partial, then it's probably better to either:
make your partial into a view, or
create a view to embed your partial.
See Rails 4 - passing variable to partial

Changing some of static content of shared partial view depending on main view

I have several shared razor partial views in my application to show some details of products on different pages. Now i need to change some of links inside partial views depending on what main view rendered that partial.
For example:
If partial view is rendered inside "index.cshtml", one of the links in partial view should be:
site1 called from index.cshtml
and if it's inside "insert.cshtml" then link need to be
other site that's not 1 and it's called from insert.cshtml
something like:
#if (something.parentview = "index.cshtml")
{ site1 called from index.cshtml}
else {
other site that's not 1 and it's called from insert.cshtml
}
Is there a way to do this?
When you call the Shared PartialView you know where are you.
Is far more easy to pass a parameter to your partial at the time of rendering that rely in "detect" inside of which view is rendered. Imagine... you have 3 levels of nesting... things get more complicated. Imagine if you change routing or view names?
You can use the ViewBag to pass the parameter: in your view you can set something to check inside the Partial. Not controller code is needed.
A more robust approach is pass a ViewModel to your Partial, but if the only data you need to pass is just a parameter, the ViewBag is simpler.

how can i call index action of default controller from my generated controller or how can i render index view from my controller?

I am not getting how can I render index view of default controller from my generated controller action.
Is there any way to do that in grails. Please let me know.
With advance thanks,
There's several way to do that. The simplest case is that you should name your view according to the controller/action that handle it.
For example, if your controller is HomeController with action index, your view should be /view/home/index.gsp. This's called Grails philosophy - Convention Over Configuration.
If you want to make an action render a specific view, you can use render command.
You should easily figure it out with the Grails document - Web layer.

can I have a controller associated with a partial?

In rails, page templates have their own controller which is called before a page is rendered (I think?).
Likewise, can I have a controller associated with a partial that is called before the partial is rendered?
I know you can pass local variable into a partial, but I want to run a good few lines of code to assign those local variables, which if I wasn't using a partial I would have put in the controller.
This code that you want to run should be in a helper that can be called from the controller, or from the partial itself.

Resources