Razor implementing template to wrap objects - asp.net-mvc

The goal es to have a template :
<div class="container">
<div class="row">
<div class="col-lg-11">
#RenderBody()
</div>
</div>
</div>
and in normal cshtml:
#using ("template") {
// Any html razor content
}
#using ("template") {
// Other different html razor content
}
Is there anyway to achieve this using cshtml for templates (no c# or html writers) ?
I know it can be accomplished with partial views, but I want to avoid to create a partial view for every piece of content I want to wrap with that template.
Thanks.

Partial Views or js Templates as mustache or handlebars.
I do not think you can assign Template with razor syntax # to another server variable inline
you also can create Razor Helpers but if you do not want to use partialViews not sure if u want to do that
#helper MyTemplate(){
<div class="container">
<div class="row">
<div class="col-lg-11">
#RenderBody()
</div>
</div>
}
and use it as #HTML.MyTemplate

Related

How to load View with PartialView already loaded in it?

I am working on this ASP.NET MVC Core project where I want to a View to be rendered on the screen with the PartialView already rendered in the View. In other words,
I have the following View:
View: Index.cshtml
...
...
<div class="card">
<div id="divPageLoad"></div>
</div>
PartialView: Details.cshtml
...
...
// Some code
...
...
I want to render Details.cshtml in divPageLoad of Index.cshtml when Index.cshtml loads on the screen. In other words, it should seem as if the contents of Index.cshtml and Details.cshtml exist on one page, where in reality, they are two different Views.
I am presently making it work in the following way:
<div class="card">
<div>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" id="lnk_InventoryDetails" onClick="GetInventoryDetails()">Details</a></li>
<li><a data-toggle="tab" href="/Inventory/Locate">Locate</a></li>
</ul>
</div>
<div id="pageLoadId"></div>
JavaScript Code:
<script type="text/javascript">
function GetInventoryDetails() {
$('#divPageLoad').load("/Constructor/Action");
};
</script>
I have used onClick(). But I know that in this way, the page will load only after thatButton is clicked. Else it won't run. I basically want to replace onClick() with something else, or take a different approach.
Here is an answer as requested:
<div class="card">
<div id="divPageLoad"></div>
</div>
#Html.Partial("PartialViewName")
OR
#Html.Partial("PartialViewName", Model) //If you need to pass a model object to partial

ASP.NET MVC - Reusable Control with Content

I have an ASP.NET MVC app. In my app, I have multiple layout files (~10). I have a block of HTML that is similar across the layout files. For that reason, I want to put it into a reusable control, because I anticipate some change will be needed to it.
On some of the pages that reference my layout files that use this control, I want to insert something into that a content area of the control. To demonstrate, imagine I have a layout file like this:
_Layout.cshtml
#** MY REUSED BANNER CONTROL GOES HERE **#
<div class="container">
#RenderBody()
</div>
Banner.file [not sure what to use]
<div class="container-fluid">
<div class="row">
<div class="col-lg-3">
Hello
</div>
<div class="col-lg-3">
#RenderSection("bannerContent", required: false)
</div>
<div class="col-lg-3">
Leave
</div>
</div>
</div>
MyPage.cshtml
#{
Layout = "~/Views/Shared/_Layout.cshtml"
}
<div>
Main content goes here
</div>
#section bannerContent {
<div>This goes into Banner.file</div>
}
The above code does not work. It is there to communicate the idea. From my understanding, a partial view won't work because partial's don't support sections. What are my options here?
If I could understand you need something like this.
Create _banner.cshtml in ~/Views/Shared.
<div class="container-fluid">
<div class="row">
<div class="col-lg-3">
Hello
</div>
<div class="col-lg-3" id="banner_content"> // Here your page specific content will go
</div>
<div class="col-lg-3">
Leave
</div>
</div>
_LayOut.cshtml
#Html.Partial("_banner"); // Reusable banner html
<div class="container">
#RenderBody()
</div>
MyPage.cshtml
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
Main content goes here
</div>
#section Scripts {
$(document).ready(function(){
$('#banner_conten').html('Your dynamic part.');
});
}
I am not sure whether I understood your requirement completely or not. But check if it helps you.

How can I create a re-usable custom HTML panel in ASP.NET MVC?

I am trying to create a re-usable UI element in ASP.NET MVC that wraps around whatever content I choose to place in it. My first thought was to create a partial view, but that doesn't seem to help me in what I am trying to accomplish.
The Template
For example I would like to create some re-usable markup similar to this (please excuse the pseudo-code):
<div class="panel panel-default">
<div class="panel-body">
#RenderBody()
</div>
</div>
A Simple Example
Then in my views I would like to do something similar to this:
#Render.Partial("MyPanel")
{
<img src="image.jpg" />
}
Assuming that code worked as expected it would generate the following:
<div class="panel panel-default">
<div class="panel-body">
<img src="image.jpg" />
</div>
</div>
Another Example, with more complex markup
Theoretically I would like to be able place any and everything I would like within one of the panels. So even more complex markup would still appear legible.
#Render.Partial("MyPanel")
{
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" id="exampleInputFile">
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
}
Just like in the first example, it would wrap everything inside the header and footer of my template:
<div class="panel panel-default">
<div class="panel-body">
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>
.. truncated ..
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
Known Possibilities
Partial View. A partial view seems to make sense, except I have been unable to figure out a way to create a partial that has flexibility beyond the Model it was designed for. I gave two examples, because the contents within my panel could be significantly different each time I use it. I would like to be able throw anything into the body of this partial view; text, markup, more razor code, etc.
HTML Helper. This works great when I have specific parameters I would be passing in. I don't see this as practical when needing to enter large amounts of markup, as it would become hard to read/maintain. Also since html would be passed in as a string, I'm not sure how this would work with razor tags?
2 Partial Views. Using one as the header and the other as the footer. This works, but it just feels sloppy to me.
I was able to do this in ASP.NET WebForms by creating a custom control. It seems like this is something that should be straight forward in MVC too, but I just haven't quite been able to figure out a good solution yet.
You can use "DynamicInvoke" to create html helpers with dynamic markup, such as:
public static IHtmlString CustomPanel(this HtmlHelper htmlHelper, Func<object, object> panelMarkup = null)
{
return CustomPanel(htmlHelper, (panelMarkup == null ? "" : panelMarkup.DynamicInvoke(htmlHelper.ViewContext).ToString()));
}
public static IHtmlString CustomPanel(this HtmlHelper htmlHelper, string content)
{
TagBuilder div = new TagBuilder("div");
div.AddCssClass("panel panel-default");
TagBuilder innerDiv = new TagBuilder("div");
innerDiv.AddCssClass("panel-body");
innerDiv.InnerHtml = content;
div.InnerHtml = innerDiv.ToString();
return new HtmlString(div.ToString());
}
And then you would use it like this:
#Html.CustomPanel(
#<text>
<div>My Custom Markup</div>
<span>More Stuff</span>
</text>
)
Your final content will look like:
<div class="panel panel-default">
<div class="panel-body">
<div>My Custom Markup</div>
<span>More Stuff</span>
</div>
</div>
You see this used often in some 3rd party widget libraries (kendo-ui is the main one i know of)
There are many ways to create reusable content in MVC, but they all have various pros and cons. For instance, custom Html helpers are a common method, but they require writing a lot more code than you probably want.
You can certainly use partial views, just pass a model containing the content you want to render to the partial... but that can also be a little confusing as well.
I think what you may be looking for are Razor Helpers. These are explained in more detail here:
http://weblogs.asp.net/scottgu/asp-net-mvc-3-and-the-helper-syntax-within-razor
However, the basic structure would look like this:
#helper MyHelper(string content)
{
<div class="panel panel-default">
<div class="panel-body">
#Html.Raw(content)
</div>
</div>
}
Then you use it like this:
#MyHelper("<img src="image.jpg" />")
You don't have to use strings, you could use structured content, models, etc.. you can use other helpers inside. You can re-use them as described later in the article.
You might want to start thinking about your pages as more data-centric, however. If you use EditorTemplates to render your data then you render controls based on the type of your data rather than rendering your pages and trying to adapt your pages to contain your data.

load partial model after ajax - mvc

I have page containig partial views
<div class="window">
<div id="progress" class="progress">
</div>
#*<div id="tlv-dialog-content" >*#
<form id="___iskadetails">
<div id="iska-general-details">
#Html.Partial("_workGeneralDetails_EditForm", Model)
</div>
</form>
<div id="tabstrip" class="tabstrip">
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
</ul>
</div>
each partial in the tabstrip contain different model
i want to load the partial view after spacific ajax im doing in the client side
in the document.ready
what is the best way to do it
Use the JQUERY UI tab and load different Partial view with their model.
pls look following links that will help you.
http://kevgriffin.com/using-jquery-tabs-and-asp-net-mvc-partial-views-for-ajax-goodness/
http://ericdotnet.wordpress.com/2009/03/17/jquery-ui-tabs-and-aspnet-mvc/

MVC4 have a view working as a layout/masterpage

As Im building sort of a window/tool setup in MVC4 Im wondering is it possible to do like a #Html.Partial("_PartialToolWindow") In a page including all the tools to be loaded.
And then have this _PartialToolWindow load a defalut html template that should surround this code that the _PartialToolWindow contains?
For example...
<div class='boarder'>
<div class='innerBorder'>
--HERE IS THE SPECIFIC CONTENS--
</div>
</div>
This _PartialToolWindow should have loaded this border and innerBorder to surrond its own HTML.
You can use a section in your layout.
_Layout.cshtml
<div class='boarder'>
<div class='innerBorder'>
#RenderSection("PartialToolWindow")
</div>
</div>
Then you will be able to load specific content in your views:
#section PartialToolWindow
{
<span>content</span>
}

Resources