Partial content accepting HTML as parameter in ASP.NET MVC 3 - asp.net-mvc

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.

Related

Using #Html.Partial In Sitecore

I would like to use the generic razor helper function Html.Partial to render views that have common html in them.
For instance, I have two views set up in Sitecore Payment Information.cshtml and Agent Payment Information.cshtml. These are rendered using the Sitecore rendering engine. Both of these views have very similar html in them that I would like to put in razor views not set in Sitecore and call them with #Html.Partial as appose to #Html.Sitecore().Rendering() as the latter forces me to set up a view and model in Sitecore which I am not sure is necessary.
My question is, is there anything that Sitecore does behind the scenes that makes it necessary to usethe #Html.Sitecore().Rendering() helper method instead of the #Html.Partial() helper method? Everything seems to work fine and I believe the entire view should get cached since the #Html.Partial call is nested inside either the Payment Information view or the Agent Payment information view set up in Sitecore.
Thanks in advance.
I have Html.Partial working in an MVC solution using Glass for ORM. There are two ways I've used this, one where the assumed model being passed to the partial is the same as the parent rendering and another where we create the model on the fly.
Assumes parent rendering model is passed:
#Html.Partial("~/Views/Components/MyPartialView.cshtml")
Instantiates a new model that is passed in:
#Html.Partial("~/Views/Components/Navigation/SecondaryNavigationRendering.cshtml", new SecondaryNavigation())
The parent view will need to have a mapped model in Sitecore. The secondary view does not have a mapped model in Sitecore but is typed to receive the model being passed (so in my first example that would be my IBasePage model, in my second it would be my SecondaryNavigation model).
Hope this helps.

Passing JS objects to an MVC Partial?

I have an observable that gets modified in the main view. In a "Preview" below, I render an MVC partial (this preview is re-used in many places throughout the app) displaying some of that observable data in real-time as it gets updated.
When I call the partial:
#Html.Partial("_TitlePanel", Model)
How can I also pass a knockout / js object to the Partial?
You don't need "pass a knockout js" in your partial view. On the client side, no main and partial views, only one html page. So, you should add data-binding in your partial view, like as in main view.

Do we have user control and hidden field in razor?

In Asp.net , there is user control, which is an ascx page, and we can have hidden fields:
public partial class classA:System.Web.Mvc.ViewUserControl<Models.classB>
{
//hidden field
public string url
{
get{ ...... }
}
}
But now i am using razor in asp.net mvc3 ,which is cshtml file. Do we have anything corresponding to that? How can i use hidden field now?
MVC views do no have fields like a Web Forms user control.
MVC views (partial views, too) get their data by passing an instance of a Viewmodel class to them. You can indicate the type of the viewmodel class with the #model directive (usually the first line in a view):
#model MyViewmodelClass
<span>#Model.MyClassProperty</span>
The viewmodel class itself could contain private fields like any class.
If you need variables to be used within the view itself, you can simply declare them within a razor code block and use them wwithin the view:
#{
var privatevar = "value";
}
<span>#privatevar</span>
However, as a best practice, the view's responsibility in ASP.NET MVC should be limited to the display of the data and therefore not contain code unrelated to that.
While working with razor it is essential to have basic HTML knowledge as you need to write some html and there is no and Drag and Drop to add controls on the page.
MVC have introduced Partial Views which can be said a alternate/replacement to User Control.
You can create a PartialView by selecting Checkbox stating "Create Partial View" in View Creation Dialog. Secondly In MVC it is not difficult to create partial view mannual as well.
As you are using Razor, just set Layout = null; at the top of your view and it will be treated as partial view. Secondly you can easily make any field as hidden using display property under Style attribute.
Happy Coding :)

Rendering html text from model inside view

In my mvc application, I have a view in which the content for the view will have to be rendered in html format. html string will be obtained form the model in the view. How can i rener the html mark up as such in view?
Thanks in advance
Have a read through: http://www.asp.net/mvc/tutorials/asp-net-mvc-views-overview-cs to see an example of the ASPX view engine rendering HTML.
You shouldn't be building HTML in the model (or in the controller and putting it into the model).
Update: as Jamie Dixon has mentioned, there are situations in which business properties of the model will contain HTML.
Generally, speaking:
The model should contain properties and business logic.
The views will render the HTML that you need.
The controller calls the business logic needed and passes the model to the correct view.
If, in the view, you wish to write out a heading based on a property called Title on the Model, you can use this:
<h1><%: Model.Title %></h1>
or if you are storing a HTML string in a property called StoredHtml:
<div>
<%= Model.StoredHtml%>
</div>
Note that <%= has been used rather than <%:, as <%: will encode HTML for security reasons (like <script> tags being saved).

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