I'm trying to create a custom HTML Helper to help simplify my masterpages menu, however it is not rendering on the HTML when I use it.. I'm thinking I will need to create a partial view, any ideas?
I did this..
public static string CreateAdminMenuLink(this HtmlHelper helper, string caption, string link)
{
var lnk = TagBuilder("a");
lnk.SetInnerText(caption);
lnk.MergeAttribute("href", target);
return lnk.ToString(TagRenderMode.SelfClosing);
}
Now in my View, i have
<% Html.CreateAdminMenuLink("Home", "~/Page/Home"); %>
Thanks: Dave Swersky
Fix was: I forgot the equals and removed the semi-colon
<%= Html.CreateAdminMenuLink("Home", "~/Page/Home") %>
but when I look at the source, its empty.. tried adding <% using (Html.BeginForm()) %> and it adds a form.. but the link still doesnt come up.. debugged and the string works when i look at the watch, but does not render..
Any ideas?
Modify your markup:
<%= Html.CreateAdminMenuLink("Home", "~/Page/Home") %>
The equals sign and no semicolon should do the trick.
Related
I have a stongly typed view:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IPagedList<Product>>" %>
I'm trying to use an HTML helper on it that works fine on a non-strongly typed view. I've tried a few variations, such as:
public static string Pager(this HtmlHelper helper)
{
//works great from non-strongly typed views
return "Pager";
}
public static string Pager1<TModel>(this HtmlHelper<TModel> helper)
{
//might work for a stongly typed view?
return "Pager1";
}
Tried:
<%=Html.Pager() %>
<%=Html.Pager1() %>
<%=Html.Pager1<IPagedList<Product>>() %>
I get similar messages:
'System.Web.Mvc.HtmlHelper<WillowCore.Common.IPagedList<Willow.Domain.BI.Product>>' does not contain a definition for 'Pager1' and no extension method 'Pager1' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<WillowCore.Common.IPagedList<Willow.Domain.BI.Product>>' could be found
What is the correct syntax?
[This is under MVC2 RC]
rookie mistake -- just forgot to import my namespace in the web.config.
for the record, either work:
<%=Html.Pager1() %>
<%=Html.Pager1<IPagedList<Product>>() %>
Is there a way to output select list to a string in the View ?
//Javascript
var comboHtml = <%= Html.Encode(Model.MySelectList.ToHtml()) %>
or would i have to enumerate the list my self to create the html,
need this for a dynamic form field.
var comboHtml = '<%= Html.DropDownList("name", Model.MySelectList) %>';
You could use ajax and Html.RenderPartial.
(assuming mvc.net btw)
Also, the issue with doing it in Javascript is that the <%= stuff here %> would render the string at the page load.
I'm with a problem, I have a ajax link that pass a parameter, but, the page that it opens does not need that parameter. The page only load 2 partial views, one of those need that parameter passed to the page to load the data correctly, and the other just need to load a form, so, don't need that parameter. How can i acheive this?
In order to do what you want, you will need to add the id to the ViewData construct.
var sysfunctions= UnisegurancaService.FunctionsRepository.All();
ViewData["NeededID"] = id
return View(sysfunctions);
then in your view where you render the partial
<%= Html.RenderPartial("GridFunction", (int)ViewData["NeededID"]) %>
Cast as required of course.
Whatever gets pushed in as the second param becomes the .Model in the partial. I would suggest also strongly typing your partials.
Try this:
<% Html.RenderPartial("GridFunction", new ViewDataDictionary {{"Id", ViewData["Id"]}}); %>
UPDATED:
And add this in your controller action:
ViewData["Id"] = Id;
UPDATED:
And in your GridFunction partial View you can access Id as:
<%= ViewData["Id"] %>
//Controller
public ActionResult EditFunctions(int id)
{
var sysfunctions= UnisegurancaService.FunctionsRepository.All();
return View(sysfunctions);
}
// This is the controller (it does no need the parameter "ID")
//This is the view "EditFunctions"
<div id="formFunction">
<% Html.RenderPartial("FormFunction"); %>
</div>
<div id="gridFunction">
<% Html.RenderPartial("GridFunction"); %> // The grid needs the ID to work correctly but its in the parent page not in the partial call....and the call is an ajax call
</div>
If some dependency of the page needs the parameter, then the page needs to know enough to pass the data in, so the page should be able to provide the data. Or, more simply, just add the parameter to the Page's viewdata and be done with it.
I have a database menu structure which I would like to add to the site.master file.
I’ve looked at other questions on StackOverflow but cannot get this to work on my website.
How do I add a User Control to the Site.Master file?
Menu.ascx
<%foreach (MainMenuSort mainMenuSort in (List<MainMenuSort>)ViewData["MainMenuSortListDisplay"])
{ %>
<li><%= Html.Encode(mainMenuSort.MainMenuId.MainMenuName)%></li>
<%foreach (SubMenuSort subMenuSort in (List<SubMenuSort>)ViewData["SubMenuSortListDisplay"])
{%>
<%if (mainMenuSort.MainMenuId.Id == subMenuSort.SubMenuId.MainMenu.Id)
{ %>
<li><%= Html.Encode(subMenuSort.SubMenuId.SubMenuName)%></li>
<%} %>
<%} %>
<%}%>
You need to use the Html.RenderPartial method in your master page.
You will need to set the MainMenuSortListDisplay and SubMenuSortListDisplay view data keys in whatever action is calling the view that uses your master page.
In your master use this
<% Html.RenderPartial("~/Views/Shared/Menu.ascx");
The path needs to be the app relative path to the control's folder. Typically these go under Shared. You can make the structure how you want below the Shared folder.
To make this technique stronger, use a strongly typed partial. In the question you would perhaps make a new class (MenuModel) with two generic collections as properties and place it in the models folder of the application. Then in the model's constructor call a method that populates the lists.
public class MenuModel
{
public IEnumerable<MainMenuSort> OuterList {get; set;}
public IEnumerable<SubMEnuSort> InnerList {get; set;}
public MenuModel()
{
VoidThatFillsTheInnerAndOuterList();
}
This will mean that you can do this in your controller
public ActionResult ShowAForm()
{
ViewData["MenuPartialData"] = new MenuModel();
return View();
}
Having set this key, your master page can use the overload of RenderPartial, like this
<% Html.RenderPartial(
"~/View/Shared/Menu.ascx",
(MenuModel)ViewData["MenuPartialData"]); %>
This assumes that your partial is strongly typed to the MenuModel class. Then in the partial you can use the model which rewrites your code slightly
<% foreach (MainMenuSort mainMenuSort in Model.OuterList) { %>
<li><%= Html.Encode(mainMenuSort.MainMenuId.MainMenuName)%></li>
<% foreach (SubMenuSort subMenuSort in Model.InnerList) {%>
<%if (mainMenuSort.MainMenuId.Id == subMenuSort.SubMenuId.MainMenu.Id)
{ %>
<li><%= Html.Encode(subMenuSort.SubMenuId.SubMenuName)%></li>
<%} %>
<%} %>
<%}%>
Hope that helps
Try something like
<% Html.RenderPartial("Menu") %>
EDIT: Corrected a typo
You could also do it as a HTMLHelper and in the MasterPage just call <%= Html.Menu() %>. Then in your HTMLHelper you have the code to get the database records and loop through them. Here is a link I found to get you started. Note my comments as there is a bug in the code example provided. I'm still having issues handling subitems of menus, I guess I need a recursive function or something??
With the help of this link. I was able to display a menu in the site.master page.
I am using the Telerik RadEditor (Q1 2009 SP1) in our ASP.NET MVC (RTM) project. The editor works great when rendered as a hardcoded object on the page with a static id. But when extending with an HtmlHelper to do dynamic creation by passing in an Id it seems to render the html as all lowercase for the tag. Does the HtmlHelper object mess with this innately by chance? The attributes look upper and lowercase respectively but this seems strange. Here is my code....thanks in advance!
<% if (placeholder.Type.ToLower() == "richtext") { %>
<%= Html.RadEditor("placeholder_" + placeholder.Name) %>
<% } else { %>
<%= Html.TextBox("placeholder_" + placeholder.Name, null, new { #class = placeholder.Type }) %>
<% } %>
The helper looks like this....
public static string RadEditor(this HtmlHelper html, string Id)
{
var sb = new StringBuilder();
sb.Append("<telerik:RadEditor ID='" + Id + "' Runat='server' DialogHandlerUrl='~/Telerik.Web.UI.DialogHandler.axd'>");
sb.Append("<Content>");
sb.Append("</Content>");
sb.Append("</telerik:RadEditor>");
return sb.ToString();
}
For the time being you cannot render RadEditor without having a valid Page object with a ScriptManager. We (Telerik that is) plan to add support for "standalone" rendering in the near future. Should be announced in a blog post so stay tuned.
The problem is the tag is a server side control. When you place it hardcoded in your page, the server side tag gets translated to html. When you're using the htmlhelper, you're outputting the html and it doesn't get processed as a server side tag.
If you want to do something dynamic, you should use a UserControl (.ascx file) and then use the Html.RenderPartial method.