JavaScript/jQuery insert ASP.NET MVC ViewUserControl into form - asp.net-mvc

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.

Related

How to load dynamically user control/ partial view dynamically in asp.net mvc

i am new in mvc...now learning but for a long time i am attach with asp.net web form technology. many way we can load user control in webform.
1) suppose when user click any button then a postback occur and a server side method call. from that server side method we can instantiate user control or load user control and add it to page from code behind.
2) another way we can load user control dynamically by jquery. we can call server side function by jquery. and from that function we can load user control and get the user control html and send that html of the usercontrol to jquery function as return result.
so i believe the same thing can be done in mvc too. so discuss all the possible way to load partial view dynamically at client side from action method and also jquery.
how to get the html of partial view here from action method ? please discuss point wise and with sample code.....because i want to learn all good tricks.
Depending on your requirements there are a few scenarios available to you:
1) Utilize a combination of Javascript and jQuery to do an ajax call, get a JSON result, and then reneder the control from a call to a partial method and $("#element").html({jsondata}).
2) Utilize the AJAXForm object to present a form that will be replaced on submission with your desired user control (called from a partial).
3) Pre-render the partial but have it hidden and on successful submission display the hidden control, or update and show depending on your needs.

Partial content accepting HTML as parameter in ASP.NET MVC 3

I have a concept of a modal window in my MVC application that renders some HTML that wraps content that will eventually show inside on the modal. The content will be HTML either directly hard coded in the view or generated by an Html helper as passed in as a parameter.
I'd like to wrap the modal content in a Razor template or partial in my application to avoid spreading it all over my application as it used in a number of pages. What's the easiest way of doing this? Can I achieve something similar in i partial view with out a model?
Build a viewmodel for the partial view... a simple C# class with a string property for the Html... using the [AllowHtml] attribute on it will let you stuff HTML in the object.
In your parent view, stuff your HTML into this view model and pass it to the partial when you call it.

ASP.NET MVC2 Master Page PlaceHolder visibility

asp:PlaceHolders have a visible property, and this controls whether their content is rendered on the page.
I have declared a PlaceHolder in an MVC2 Master Page and set it's visibility to false.
Please can you tell me how I can control the visibility of a PlaceHolder from within an MVC2 view template that inherits from an MVC2 master page?
This seems like a simple task, but I am struggling to see how it can be achieved. I think I should be able to get access to the PlaceHolder from within the descending view template (as with web forms code-behind) and just set it's visibility there but the way to do this is escaping me...
Don't do this. Placeholders are legacy from classic WebForms. Manipulating server side controls in an ASP.NET MVC application is very bad and you should never do it. So simply forget about setting properties on user controls. Fortunately in Razor placeholders have been completely removed and replaced by sections. So don't write code that you won't be able to migrate later.
One way to show/hide sections of your code in an ASP.NET MVC application is to use an if statement in your views. For example:
<% if (Model.ShouldShowSection) { %>
<div>Some super section</div>
<% } %>
In this example we are testing a boolean value on the view model which the controller action that rendered this view would set.

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.

ASP .NET MVC: Html.Radiobutton onclick -- set ViewData[""]

I'm new to ASP .NET MVC and to web programming in general.
I'm wondering if there is a way to set ViewData variables when a radiobutton is selected -- but before the page is submitted.
Maybe I'm barking up the wrong tree but what I'm trying to do is create a form where new fields are added based on which radio button is selected. So what I want to do is when a radiobutton is clicked it sets a ViewData variable and based on that ViewData variable a different partial view loads the appropriate fields below the current field.
I imagine there must be someway of doing a onclick="some C# function that sets ViewData(args)"
Thanks
There are a couple of ways you could go about this.
1) You could have an Ajax form where through Javascript you post the form back and check to see if it's an Ajax Request, there by returning a partial view to a div that you specify.
2) Post the form as is and check server-side to see if the radio button was clicked, and thus redisplay the form with the new options visible.
If you take the first approach it would be easy enough to fall through to the second one for those without Javascript enabled.
There aren't really "onclick" events as I'm assuming you are used to from Webforms, you would basically have to roll your own Javascript to handle such things. Once you do a few, I think you'll find it's really not too bad, with the benefit that you'll have more control over what you're doing and through that gain a better understanding of the larger picture.
ViewData only exists, and only exists server-side, for the lifetime of the request. So, once the page is rendered the object no longer exists.
Some alternate approaches you can take:
1 - Use client-side Javascript to add a form and inputs as necessary. More info here:
ASP.NET MVC & JQuery Dynamic Form Content
2 - Pre-render the new form, but hide it via CSS, and unhide it when the appropriate radio button is clicked. More info here:
expand collapse html field Firefox
3 - Use AJAX to render the new form when the appropriate radio button is clicked. More info here:
http://www.asp.net/learn/mvc/tutorial-32-cs.aspx

Resources