I use the version 4 of Umbraco, and wondering if there is a way to render a module from an other module.
say
Root
=> Folder1
=>=> Page1
=>=>=> PageLayout
=>=>=>=> MainContentZone
=>=>=>=>=> MyMainModule
=>=>=>=>=>=> MyChildModule1
=>=>=>=>=>=> MyChildModule2
=>=>=>=>=>=> ...
=>=>=>=>=>=> MyChildModuleX
I would like to iterate over child modules from MyMainModule and display them all.
Something like this
#{
var myLayoutHelper = new LayoutHelper(this);
var modules = myLayoutHelper.CurrentModules;
}
#foreach (var mod in modules)
{
<div>
#myLayoutHelper.RenderModule(mod)
</div>
}
Is it possible?
Yes, you can call an other macroScript from a macroScript
#foreach(var mod in modules)
{
#RenderPage("~/MacroScripts/Mod-Detail.cshtml", mod.Id);
}
and then in the Mod-Detail page you can pick up the id like this:
int myId = PageData[0];
Update:
For those using Umbraco v6 in MVC modus try
#Html.Partial("PartialName")
#Html.CachedPartial("PartialViewName", model:myModel, cachedSeconds:60, cacheByPage:true, cacheByMember:false, viewData:AViewDataDictionary)
#Umbraco.RenderMacro("MacroAlias") // to render a macro
Related
I have a chat side bar that I would like to populate dynamically from the identity users. The below script will display my name 5 times but I would like the list of users online to be displayed through this function.
//creates markup for a new popup. Adds the id to popups array.
function populatesidebar() {
var sidebarNameHtml = '';
for (var iii = 0; iii < 5; iii++) {
sidebarNameHtml = sidebarNameHtml +'<div class="sidebar-name"><img width="30" height="30" src="#Url.Content("~/Images/Jodaine.PNG")"/><span>Jodaine</span></div>';
}
$('.chat-sidebar').html(sidebarNameHtml);
}
I am using the registration system comes with the project when selecting single user authentication. To get the user through to the html I used
using (ApplicationDbContext db = new ApplicationDbContext())
{
return View(db.Users.ToList());
}
But I would like to manage the users through the script.
You need to reference the model in view, something like:
#model List<WebApplication1.Models.User>
Then you need to write some javascript dynamically. Something like:
function populatesidebar() {
var sidebarNameHtml = '';
#foreach (WebApplication1.Models.User user in Model)
{
#: sidebarNameHtml = sidebarNameHtml +'<div class="sidebar-name"><img width="30" height="30" src="#Url.Content("~/Images/Jodaine.PNG")"/><span>Jodaine</span></div>';
}
$('.chat-sidebar').html(sidebarNameHtml);
}
Some of this is static but there is some razor that loops through your model inserting some User properties into the javascript. You were trying to write some javascript that loops through the model. The javascript will be executed in the browser, on the client, which knows nothing of your model. You need to execute razor on the server that writes the javascript you need to the page that is sent to the browser.
I have returned a list of warning classes from the control class like this:
public ActionResult Ex3()
{
List<warning> warningsList = new List<warning>();
XElement xelem = XElement.Load(transformedFile);
var warnings = from elem in xelem.Descendants("warning")
select elem;
foreach (var v in warnings)
{
warning warn = new warning();
warn.id = v.Attribute("id").Value;
warningsList.Add(warn);
}
return View(warningsList);
}
i have created a view with the option "create a strongly typed view" and i have choose the warning class as a model (razor engine).
Now, in Ex3View.cshtml, i want to create and fill a gridpannel with the values in the warningsList. How can i do that ? how can i access these data in the returned list?
Please note that the view file is .cshtml and not .aspx.
You can put a Model to a Store's DataSource:
#Html.X().Store().DataSource(Model)
When I want to show an image I use something like this:
public ActionResult Show(int id) {
var MyFile = GetFromDB(id);
return File(MyFile.Data, MyFile.ContentType);
}
And in View:
<img alt ="" src='#Url.Action("show", new { id = 36 })'/>
So assume we have some other types of file like txt, pdf, ...
And I think to use a general tag to show all kind of files instead of <img>. Does any one have any idea about it?
It is not easy, because some files need initialization or need additional declarations to specify how they should be presented, but you can try with iframe:
<iframe src='#Url.Action("show", new { id = 36 })'></iframe>
It will use browser built-in way of presenting documents.
Hi i need to show a list of data using viewbag.but i am not able to do it.
Please Help me..
I tried this thing:
ICollection<Learner> list = new HobbyHomeService().FetchLearner();
ICollection<Person> personlist = new HobbyHomeService().FetchPerson(list);
ViewBag.data = personlist;
and inside view:
<td>#ViewBag.data.First().FirstName</td>
But this does not show up the value and gives error saying "Model.Person doesnot contain a defibition for First()"
In your view, you have to cast it back to the original type. Without the cast, it's just an object.
<td>#((ViewBag.data as ICollection<Person>).First().FirstName)</td>
ViewBag is a C# 4 dynamic type. Entities returned from it are also dynamic unless cast. However, extension methods like .First() and all the other Linq ones do not work with dynamics.
Edit - to address the comment:
If you want to display the whole list, it's as simple as this:
<ul>
#foreach (var person in ViewBag.data)
{
<li>#person.FirstName</li>
}
</ul>
Extension methods like .First() won't work, but this will.
To put it all together, this is what it should look like:
In the controller:
List<Fund> fundList = db.Funds.ToList();
ViewBag.Funds = fundList;
Then in the view:
#foreach (var item in ViewBag.Funds)
{
<span> #item.FundName </span>
}
simply using Viewbag data as IEnumerable<> list
#{
var getlist= ViewBag.Listdata as IEnumerable<myproject.models.listmodel>;
foreach (var item in getlist){ //using foreach
<span>item .name</span>
}
}
//---------or just write name inside the getlist
<span>getlist[0].name</span>
i had the same problem and i search and search .. but got no result.
so i put my brain in over drive. and i came up with the below solution.
try this in the View Page
at the head of the page add this code
#{
var Lst = ViewBag.data as IEnumerable<MyProject.Models.Person>;
}
to display the particular attribute use the below code
#Lst.FirstOrDefault().FirstName
in your case use below code.
<td>#Lst.FirstOrDefault().FirstName </td>
Hope this helps...
Use as variable to cast the Viewbag data to your desired class in view.
#{
IEnumerable<WebApplication1.Models.Person> personlist = ViewBag.data as
IEnumerable<WebApplication1.Models.Person>;
// You may need to write WebApplication.Models.Person where WebApplication.Models is
the namespace name where the Person class is defined. It is required so that view
can know about the class Person.
}
In view write this
<td>
#(personlist.FirstOrDefault().Name)
</td>
Just put a
List<Person>
into the ViewBag and in the View cast it back to List
This is what i did and It worked...
C#
ViewBag.DisplaylList = listData;
javascript
var dispalyList= #Html.Raw(Json.Encode(this.ViewBag.DisplaylList));
for(var i=0;i<dispalyList.length; i++){
var row = dispalyList[i];
..............
..............
}
//controller You can use this way
public ActionResult Index()
{
List<Fund> fundList = db.Funds.ToList();
ViewBag.Funds = fundList;
return View();
}
<--View ; You can use this way html-->
#foreach (var item in (List<Fund>)ViewBag.Funds)
{
<p>#item.firtname</p>
}
I had the problem that I wanted to use my ViewBag to send a list of elements through a RenderPartial as the object, and to this you have to do the cast first, I had to cast the ViewBag in the controller and in the View too.
In the Controller:
ViewBag.visitList = (List<CLIENTES_VIP_DB.VISITAS.VISITA>)
visitaRepo.ObtenerLista().Where(m => m.Id_Contacto == id).ToList()
In the View:
List<CLIENTES_VIP_DB.VISITAS.VISITA> VisitaList = (List<CLIENTES_VIP_DB.VISITAS.VISITA>)ViewBag.visitList ;
Has anyone written any HTMLHelper classes for MVC that help with Yahoo's User Interface Library?
For instance I have written a helper method to convert a 'menu model' into the HTML markup needed to support the Yahoo Menu Control. The MVC pattern works well here because obviously if I chose to switch to a different menu implementation I can just write a new helper and not touch the model.
This code works for me but isn't fully tested and you're welcome to use it.
First we need a simple data structure for the menu model itself. You would add this to your page model with the normal MVC conventions. For instance I access a list of menu items from my view via ViewData.Model.MainMenu.MenuOptions.
public class MenuItem
{
public string Text { get; set; }
public string Description { get; set; }
public string RouteURL { get; set; }
public bool SeparatorBefore { get; set; }
public List<MenuItem> MenuItems { get; set; }
}
Extension method. Put in a namespace that is accessible to your view.
public static class YUIExtensions
{
public static string RenderMenu(this HtmlHelper html, string id, List<MenuItem> menuItems)
{
// <div id="mnuTopNav" class="yuimenubar yuimenubarnav">
// <div class="bd">
// <ul class="first-of-type">
// <li class="yuimenubaritem first-of-type"><a class="yuimenubaritemlabel" href="#store">Store</a></li>
// <li class="yuimenubaritem"><a class="yuimenubaritemlabel" href="#products">Products</a>
// <div id="communication" class="yuimenu">
// <div class="bd">
// <ul>
// <li class="yuimenuitem"><a class="yuimenuitemlabel" href="http://360.yahoo.com">360°</a></li>
// <li class="yuimenuitem"><a class="yuimenuitemlabel" href="http://mobile.yahoo.com">Mobile</a></li>
// <li class="yuimenuitem"><a class="yuimenuitemlabel" href="http://www.flickr.com">Flickr Photo Sharing</a></li>
// </ul>
// </div>
// </div>
// </li>
// </ul>
// </div>
//</div>
int menuId = 0;
HtmlGenericControl menuControl = CreateControl(html, id, 0, ref menuId, menuItems);
// render to string
StringWriter sw = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(sw);
tw.Indent = 1;
menuControl.RenderControl(tw);
return sw.ToString();
}
private static HtmlGenericControl CreateControl(HtmlHelper html, string id, int level, ref int menuId, List<MenuItem> currentItems)
{
var menu = new HtmlGenericControl("div");
menu.Attributes["class"] = (level == 0) ? "yuimenubar yuimenubarnav" : "yuimenu";
menu.Attributes["id"] = id;
var div_bd = new HtmlGenericControl("div");
menu.Controls.Add(div_bd);
div_bd.Attributes["class"] = "bd";
HtmlGenericControl ul = null;
int i = 0;
foreach (var menuItem in currentItems)
{
if (ul == null || menuItem.SeparatorBefore)
{
ul = new HtmlGenericControl("ul");
div_bd.Controls.Add(ul);
if (i == 0)
{
ul.Attributes["class"] = "first-of-type";
}
}
var menuItem_li = new HtmlGenericControl("li");
menuItem_li.Attributes["class"] = (level == 0) ? "yuimenubaritem" : "yuimenuitem";
if (i == 0)
{
menuItem_li.Attributes["class"] += " first-of-type";
}
ul.Controls.Add(menuItem_li);
var href = new HtmlGenericControl("a");
href.Attributes["class"] = (level == 0) ? "yuimenubaritemlabel" : "yuimenuitemlabel";
href.Attributes["href"] = menuItem.RouteURL;
href.InnerHtml = menuItem.Text;
menuItem_li.Controls.Add(href);
if (menuItem.MenuItems != null && menuItem.MenuItems.Count > 0)
{
menuItem_li.Controls.Add(CreateControl(html, id + "_" + (menuId++), level + 1, ref menuId, menuItem.MenuItems));
}
i++;
}
return menu;
}
}
Stick this code where you want to generate the menu in your view (I have this in a master page):
<%= Html.RenderMenu("mnuTopNav", ViewData.Model.MainMenu.MenuOptions) %>
If you're lazy, or don't know about YUI you'll need this too in your <HEAD>
<!-- Combo-handled YUI CSS files: -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.6.0/build/menu/assets/skins/sam/menu.css">
<!-- Combo-handled YUI JS files: -->
<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js&2.6.0/build/container/container_core-min.js&2.6.0/build/menu/menu-min.js"></script>
This currently generates markup for top nav style navigation bar - but it could be easily modified.
I was hoping somebody else was doing the same for some of the other controls.
Seems like a good candidate for an open source project - but I dont have time to start that.
Implementation advice welcomed!
Last night I did some thinking about this and am wondering if there's even more opportunity here to make general purpose HTMLHelpers using YUI or whatever other Javascript/HTML widgets you want.
For instance, if there was an interface for IMenu and one for ITextBox, ICheckBox, IRichTextEditor, ICarousel, etc. much like your class for a MenuItem, then you could have a YUI implementation of each of those interfaces, one for JQuery, one for MooTools or one for just straight HTML/CSS.
Part of what sparked this is the generalization that articles like this: http://designingwebinterfaces.com/essential_controls are taking to UI controls on the web for rich web apps.
Those interfaces would contain all of the basic stuff that is obvious at first glance: Id, Name, Value, List, Style, OnChange, OnClick, etc. as well as less obvious stuff like ValidationRegex, HelpText, etc.
That would let you have a layer that converts a model object or model property into an ITextBox and not worry about which one of the implementations of the interface will actually be handling it. You could also easily switch to a new implementation if one came along that was better/faster/cooler.
You'd have to deal with what should happen if you give something like ValidationRegex to a barebones HTML implementation and it has no way to deal with it, but I think it's a path worth thinking about. I also think it might make more sense to implement this as separate from the existing HTMLHelper namespace by inheriting it or just reimplementing it, but I am often wrong at this kind of early idea stage of coming up with a solution.
The YUIAsp.NET stuff is interesting, but is more oriented to WebForms and user controls than the direction that ASP.NET MVC and even moreso with Fubu MVC recently are going.
I tinkered with this idea a little bit and am really intrigued with the possibilities.
Simon,
I'm not sure this is helpful with respect to the MVC question, but there is a good open-source project that aims to simplify working with YUI within .NET:
http://www.yuiasp.net/
Menu is one of the controls that they include.
At the very least, this may be a project you can contribute back to if your work adds a new dimension to what's already there.
-Eric