How to position a partial view in a page when load dynamically after postback - asp.net-mvc

i am new in mvc so like to ask 2 noob questions and those are
1) how to load partial view dynamically after postback. suppose i have a button in my main view and when user will click then i want to load a partial view dynamically after postback. i know how to load partial view dynamically using jquery but i want to know how can i load partial view after postback in main view without using jquery.
2) when we load any view dynamically by jquery then very easily we can capture the view rendered html and set or position that html in any where in my main view but i want to know how could i position or adding partial view html after postback. is it at all possible without using jquery?
in asp.net webform we can load user control dynamically after postback and also we can add that usercontrol any where in page without using jquery. the same kind of flexibility is there in mvc or is it restricted.
please give answer point wise with example or sample code. thanks

for your first question you can use different techniques, here is a sample
[HttpPost]
public ActionResult Foo()
{
ViewBag.ShowPartial = true;
return View();
}
in the view
#if (ViewBag.ShowPartial)
{
Html.Partial("_MyPartial");
}
for second question i suggest you read a little more about asp.net mvc, and postback means nothing in asp.net mvc, it is post request and get request so on, and you can easily add what ever you want to the view
one other sample
in the view
#if (Request.RequestType == "Post")
{
// do what ever you want
}

Related

How to update reportviewer in same view in asp mvc?

I want to make a asp mvc view for reportview like 2 part:
part 1: option -> buton submit
part 2: show reportview up to option (also refesh new rdlc and parameter...)
While research, i just found 3 solution for reportview in MVC:
1/ Use Web User Control, or Aspx and insert it into view through #Html.RederPartial (i dont know how to pass parameter in this case too..)
2/ Use ReportViewer for MVC https://reportviewerformvc.codeplex.com/wikipage?title=Getting%20Started
3/ Add Action for generate PDF, Excel, Word and Image File for Report Data
http://www.dotnetawesome.com/2013/09/microsoft-report-in-mvc-4.html
But both 3 solution will popup new page report when you press submit button.
How can i reload it in same page with its option (filter) ?
1) Take one parent view,within Parent view call partial view.
Parent View will have submit button and Partial view will have your Reportviewer mvc control. On button submit return partial view and if you can't able pass value to partial view then assign reportviwer data to Viewdata or viewbag and use that viewbag to bind reportviewer in partial view.
Using above approach every time you can update reportviewer.

Wrap section and main body into one form in ASP.NET MVC 3

In my ASP.NET MVC 3 project I have a master layout with a section defined. This section is responsible for displaying content in a sidebar, when it has any content assigned to it.
The problem I've encountered is the following:
I have some edit views, where both the sidebar and the main area is used for editing data. In this case the sidebar and the main body should be wrapped into one single form with a single submit button.
What is the best solution for this? The solution I came up with is that when the functionality I mentioned is necessary, I set a boolean property in my ViewBag. If this property is true, the master layout is rendered with the sidebar and the main content area wrapped in a form.
Is there a better way to this? The solution I described is a bit 'hackish' for me.
I would have two layouts. One with a seperate side-bar, and one without. Then, in the pages you need a sidebar with editable fields, you include the sidebar in your content page, not in the master.
The boolean you are using in the ViewBag can be better represented in the Model for the view of the master page. You can then include the side bar using a partial view. It would look somehting like this...
master.cshtml:
#using (Html.BeginForm("ActionName", "Controller", "POST")){
//
// Master form elements go here
//
//Side bar
#if(Model.ShowSideBar){
#Html.Partial("MySideBarPartialView" [, Model.SideBarModel ] )
#}
#}
You model (if any) should have a property that is set with the model for the side bar.

DOM Elements accessibility in ASP.NET MVC and ASCX

I have a complex asp.net mvc view where I load an "ascx" partial view into a div using $.ajax(). So I was wondering about the accessibility of the DOM elements contained in the main page and the ascx partial view. Using jQuery selectors, can I find an element in the main page from the ascx partial view? and the other way round?
Both ways, depending on the point on time.
You can always access the elements from the main view, since the HTML is bound at render-time (e.g $(document).ready())
You won't be access the elements in the partial view from the parent view until the callback from the $.ajax method has completed successfully and you have binded the HTML to the DOM (with .html(result) for example).

Rendering views without master page in MVC3

I have a few views on my website that i need to include on a start page. However, just using Html.Renderaction renders the master page and the whole shebang.
How would i go about just rendering the content?
There are a few ways.
Make sure your returning PartialView from your Controller.
return PartialView("MyView");
Or you can set Layout to String.Empty or null inside the view.
#{
Layout=String.Empty;
}

ASP.NET MVC loading multiple partial views into a single div using JQuery

I am working on a help page which is composed of a navigation tree, content box, and search box. The navigation tree has links to Frequently Asked Questions and Glossary, each of which are linked to an Action which return partial views.
To the right of the navigation tree I have a single content div that I would like to contain whichever partial view is selected in the navigation tree.
Success case (what I want to accomplish): clicking on one of the FAQ links calls the FAQ() method in my controller, then returns a partial view which is then displayed in my content div to the right of the navigation tree. Clicking on another item would cause another partial view to be loaded.
How do I go about this? I've read a ton of ASP.NET MVC JQuery loading blog posts and tutorials but can't find anyone who's done exactly this.
Many thanks!
You should be able to use the jQuery method .load() to load HTML into your div.
http://api.jquery.com/load/
You can create an action that returns the partial view as HTML.
ASP.NET MVC Returning Partial View as a full View Page
jQuery:
one easy way you can do is load all partial views in "Container Div" at page load in one go (if performance is not a issue)
then assign each partial div with different div id inside "container", than use jquery to control show(); hide(); for each div.
MVC:
however if i were you, "Glossary" and "FAQ" looks same model to me it shouldn't be put in different partial view in first place.
if they did designed in separate model, in this scenario, i would recommend you to create a proxy class as a ViewModel above models you want to display, and then load it with one partial view only

Resources