ASP.NET MVC Hybrid of ContentResult and ActionResult - asp.net-mvc

I am after creating something like a ContentResult, but have it render within the #RenderBody() tag of a masterpage. Is this possible? I can't seem to find a clear answer for it.

No; you can't do that.
Instead, you can make an empty Razor view that simply renders HTML from its model.

You can have an action method return a ContentResult, and then invoke it in the master page by doing:
#Html.Action("Action", "Controller")
I'm not sure what you mean by doing it in #RenderBody, but you can do it within the master page itself, or within each content page.
The content will be generated in the page; you could also use a partial view approach too.

Related

ASP.Net MVC Partial View Model Binding

I'm very new to MVC and I am looking to put a list of links on the main layout(master page) based on database table. I'm sure I read before that you shouldn't try to load models on the master page but use Partial Views instead (correct me if I'm wrong).
I've looked on Google and on other questions here but they only seem to talk about passing data from a main view to a partial view via ViewBag but I think I just want to add a partial view that I can add to the master page.
Can someone please tell me how to create a partial view I can add to master page so its used on every page and be able to load the list of links required i.e. by binding IEnumerable model to Partial View?
Try using ChildActionExtensions.Action
In your layout:
#Html.Action("MyAction", "MyController")
Controller:
public ActionResult MyAction()
{
var list = // get your list values
return PartialView("MyViewName", list);
}
Then just create your partial view:
#model IEnumerable<WhateverType>
#* View goodness *#
You can use this to bind whatever model you need to your partial view and if you use the Action helper in your Layout.cshtml it'll be rendered on every page.

loading partial view differently

i am new in mvc and i know only two way one can render partial view like
#Html.Partial("PartialView1")
another one is to load partial view using jquery. i like to know is there any other ways around to load partial view.
when i render partialview like this way from my action method
[HttpPost]
public ActionResult Save(string name, string salary, string btnSubmit)
{
return PartialView("TestPView");
}
then PartialView content was render in page but shared look & feel goes out from the page when PartialView shown.
what i need to include in partial view as a result common look & feel show when partial view render. please guide me and show me all the various way to load partial view. thanks
Have a look at below link. It will help u to better understand the possible ways of rendering the partial views in MVC:
http://www.dotnet-tricks.com/Tutorial/mvc/Q8V2130113-RenderPartial-vs-RenderAction-vs-Partial-vs-Action-in-MVC-Razor.html
If I understand you correctly, what you are looking for is a layout and not a PartialView.
The PartialView provides you with the option to reuse a part of the view across several views.
The layout gives you the general look and feel of the website.
Here is the default example, that the VS generates for you :
#{ Layout = "~/Views/Shared/_Layout.cshtml"; }

asp.net mvc - ascx control or what?

so I have a form using typical Ajax.BeginForm inside a typical partial view (say T.ascx).
now I am adding validtion errors to Model using
catch (RulesException ex)
{
ex.AddModelStateErrors(ModelState, "Upload");
return PartialView("T.ascx");
}
this was working fine and the user control was clearly reflecting what was causing the validation error.
Now, I want it to be a part of the page. Now, you would tell me that go ahead and add a page and reference this control as RenderAction or RenderPartial, but the page would not have anything else apart from this, so it feels odd for me to want to add an aspx page just that I can use a control. I am sure there is a better way.
EDIT: It feels odd because the only purpose page would serve is reference the user control. Is there a better way? Like to serve ascx as a page itself (but using the master page).
Your partial view won't be referencing your master page file, even if it was possible to render it without any regular view.
I don't see a problem with having a view that just renders a partial view.

MVC 2 RenderAction

Im having a value in ViewData, lets say htmlhelper.ViewData["myData"]="some";
And in partial page I can overwrite the myData's value.
But when I using the Html.RenderAction() and call a partial page.
In the same partial page htmlhelper.ViewData["myData"] is null.
When you call RenderAction, you create an entirely new ViewData instance for your partial page. If you want ViewData["myData"] to be visible by your other action, either pass it to the subaction or put it in TempData.
I figured out from MVC source code. Cool that we have MVC as open source.
htmlHelper.ViewContext.HttpContext.Items["myData"]
this will maintain the value from Partial and RenderAction case.

JavaScript/jQuery insert ASP.NET MVC ViewUserControl into form

I have existing ASP.NET MVC View pages and View user controls which I currently use in normal straightforward ASP.NET MVC fashion, sometimes I use RenderPartialView or RenderAction, etc.
By themselves they include tag. I would like to dynamically load either Views or ViewUserControl based on the selection in a dropdown list.
I'm having trouble deciding should I remove from Views and controls and put it just into the one View that will do dynamic rendering or to leave it there and leave outside of the .
What do you think and how would you go about it?
I would probably try to load the contents of a div after doing an AJAX call to get the contents. See the AJAX get call in the jQuery docs.
Or are the possibilities of what control to load so small you could just hide/show div's that are already in the page?
You can use JQuery to get the HTML from your Partial views and substitute it in the div. It could be something like this:
$.get('/Controller/Action',function(data){
$('div').innerHtml(data);
});
I did it this way and it works. /Controller/Action can be a partial view which returns HTML.

Resources