As per requirement, i need to retrieve html page layout from database (that system user can update through system) with some html helpers (Dropdown, TextBox etc).
<html>
<body>
<div>
#Html.DropDown(x=>x.SomeId,Model.ElementCollection)
</div>
<div>
#Html.TextBoxFor(x=>x.Property)
</div>
</body>
</html>
In the above example, i need to get html from database but before render the page i want added some html helper's.So it will work like formal page.
Tried Solution:
For this i have created Custom View Engine and i am replacing text into Render function of IView inherited class.but its not resolving html from helper.
Question:
Is there any to get html from htmlHelper.If yes, then i can simply replace string into action method and return Content from action method?
Suggest please.
I found some useful link, to implement same functionality with Custom View Engine and Virtual Views.
Link1 And Link2
Thanks :)
Related
I have a Razor Pages layout page that includes a nav bar as a partial page
<html>
...
<body>
...
<partial name="_Nav" />
Inside my _Nav.cshtml
<div class="links">
link 1
link 2
<!-- custom links that are set by each page go here-->
</div>
This layout is used by every page in the website. I would like to be able include "extra" links that pertain to each page in the website.
I've tried doing it was #RenderSection, but it seems that sections are only allowed in the Layout page. Does this mean I need to do away with my _Nav partial and and lump all the code into one file? Or is there a way to keep an organized code structure and still pass some code around? With jinja2 code blocks this is no problem, so I'm hoping there is a nice way in Razor Pages as well!
Also, I really don't want to pass full html strings from the c# class out to the html, I'm already passing out any variables I need in the links.
Thanks for your help!
You don't have to store html in your ViewDataDictionary.
On every view that has extra links to add, store a List<string>, strings being urls, something like this:
View:
#{
ViewData["Links"] = new List<string>(){ "https://www.google.com", "https://www.facebook.com" };
}
Then in your Layout view:
<partial name="_Nav" view-data="#ViewData" />
Now in your partial view:
//Default Links
#if (ViewData["Links"] != null)
{
//We have extra links
List<string> links = (List<string>)ViewData["Links"];
foreach (string link in links)
{
link1
}
}
RenderSection can only be called from a layout page.
There is no such plugin to add the dynamic link to the partial view.
As a workaround,you could put a div outside the _Nav.cshtml like below and use RenderSection to dynamic add link to the div:
....
<div class="links">
<partial name="_Nav" />
#RenderSection("Html", required: false)
</div>
....
_Nav.cshtml:
link 1
link 2
Test View:
#page
#model IndexModel
//do your stuff...
#section Html
{
<a href='#link3'>link3</a>
}
So as of right now, I'm using my _Layout.cshtml page to build the header, body and footer, and in the bodu I'm doing this in order to render my Index page:
<body class="darkDesign">
<div class="container-fluid">
#RenderBody()
</div>
</body>
And my index page is just the default one that comes with the template:
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about building Web apps with ASP.NET Core.</p>
</div>
The issue is that I want to add a class to the body tag but it won't render it because it seems as RenderBody dynamically renders the body tag for me and this is what it looks like in the DOM editor in Chrome.
How do I properly add the class to the body tag?
The problem in not Razor, as #Wiktor-Zychla mentioned, RenderBody doesn't dynamically render the body tag. (the Body in RenderBody means the page/layout content/body)
Also as I mentioned in the comment section, the <!--Loader starts--> part is not in your source (Razor), so darkDesign class removed by JS when you visit the page in the browser. as you confirmed, you should be able to see darkDesign class in body tag in page source. (in your browser hit Ctrl + U to see the page source)
So
If you using some JS library/framework (or jQuery plugin), you should check their documentation to see why they removed class from body. maybe you can disabled this behavior or tell them to add your class.
Another way (easy and dirty one) is adding the class using JS, just make sure your code execure after their code.
You can mention which library/framework (or jQuery plugin) you using, then maybe I can help you more.
I try to understand how works sections in Razor views. I have 2 layout files: _Layout.cshtml and _LayoutChild.cshtml which use the first layout inside. I try to declare sections in _Layout.cshtml and render it in the same file. Markup not appears but when I declare section in _LayoutChild.cshtml everything works. See example below:
_Layout.cshtml:
<!DOCTYPE html>
<html>
<head>
#RenderSection("A", false)
</head>
<body>
#section A{
<script>
alert("I'm in Layout!");
</script>
}
</body>
</html>
_LayoutChild.cshtml
#{
Layout = "_Layout";
}
#section A{
<script>
alert("I'm in LayoutChild!");
</script>
}
I understand that declaring section in the same file looks strange but I would like to know why it don't work?
While developing web site or application. Every page have some common section like header, footer, sidebar etc. Write and maintain these on every page is a hard job. So, we need a way to put them in one single place. Luckily in MVC we have Layout concept for this. We put all the common code in it and changing content in Views which will render in layout where we call #RenderBody() method.
Why we need sections ?
Some time we wanted to show specific content on some specific pages like newletters only show on blogs and home page, top news or some advertising banners on specific pages. For these type of scenarios sections rescue us in MVC.
How we can use section ?
We need to follow these two steps to used sections in MVC.
Declare Section in Layout, give it a name and tell is it required on
every page or not.
Define it in view
When views render then section content also added where we declare the section.
Code Example
1. Most of the time we need some specific javascript code for view. So we can do it like this
_Layout.cshtml
#RenderSection("scripts", required: false)
view.cshtml
#section scripts{
<script>
alert("I'm in Layout!");
</script>
}
2. News Letter example
_Layout.cshtml
#RenderSection("newletter", required: false)
view.cshtml
#section newletter{
<h3> New Letter </h3>
... rest of the html ...
}
So.. The issue I have came across is I am migrating a client's web app to MVC2, and the original method for displaying html content is not capable of working with MVC so I am needing to update it. The functionality I need is like so: There is a side nav that will contain the actions, and say the user clicks on "FooBar" it will populate the "mainContent" placeholder with the "FooBar.html" file from a document directory. I would like to do this with no postbacks as well if it is possible. Any ideas?
You may use jQuery ajax to load the pages when user clicks on the link. load() function will be ideal here.
It is just HTML and javascript. Nothing specific to ASP.NET MVC
//Include jQuery library
<div>
<a href="Home/about" class="aLink" >About</a>
<a href="Home/FAQ" class="aLink" >FAQ</a>
<a href="Home/Contact" class="aLink" >Contact</a>
</div>
<div id="mainContent">
</div>
<script type="text/javascript">
$(function(){
$("a.aLink").click(function(e){
e.preventDefault(); // prevent the default navigation behaviour
$("#mainContent").load($(this).attr("href"));
});
});
</script>
I have already read through this. However, in my case the page I am posting to is an external .aspx page.
Basically I generate XML data on the source page code with XElement, and need to POST that to an external .aspx page. The requirement say it needs to be wrapped in HTML </form> tag before posting. So my string in the code-behind file looks like
<FORM id="frmLogin" action="https://illustration.sagicorlifeusa.com/fse5/main/FormPost.aspx" method="post" target=blank>
<XML>myxml<XML\>
<input type="submit" name="__exclude__Submit" value="Run Sagicor Life Illustration Software Online ">
</form>
Now on the code-behind file, what is it that I need to do, to post this to the external .aspx page?
You can add a hidden input, set its value in codebehind and post the form to the destination page so that on that page the value can be extracted back. Did i understand your question?
That doesn't make sense. You normally don't post HTML code.
You should check the requirement for what they really want. My guess is that you would put it in an HTML tag if you post it from the browser, but that would not apply if you post it from the server.