Render differences between html.RenderPartial and <%# register - asp.net-mvc-2

Ok, I have a master page which I include an ascx...
<%# register tagname="header" tagprefix="vb" src="~/Views/Controls/Header/Header.ascx" %>
<vb:header id="pageHeader" runat="server" />
The ascx has a site map which uses the MvcSiteMapProvider...
<asp:SiteMapDataSource id="SiteMapDataSource1" sitemapprovider="Secure" showstartingnode="false" runat="server" />
<asp:menu id="headerMenu" DataSourceID="siteMap" orientation="Horizontal" staticenabledefaultpopoutimage="false" runat="server" IncludeStyleBlock="false"></asp:menu>
Everything works nicely and then I needed to pass the model into the control so changed the master page to
html.RenderPartial("~/Views/Controls/Header/Header.ascx", Model)
Now I get a runtime error "Control '2_headerMenu' of type 'Menu' must be placed inside a form tag with runat=server." and Yes I do have a Form tag with runat=server in the master page.
Therefore does anyone know the render differences between these two approaches or any other pointers??
Thanks in advance.

Didn't think MVC will automatically find the ascx in that directory will it?
Yes pretty sure the Control is set up properly and loads OK until it tries to render the asp:menu

Try to not use the relative path but just the name of the partial view
html.RenderPartial("Header", Model);
Now, another important point is to specify what kind of object you are using in your partial view. The first line should be:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<OBJECT-TYPE>" %>
Replace OBJECT-TYPE by the object type.

The problem as one commentor noted is that you are mixing a WebForm control in an MVC view. As the error you are receiving states, the Menu has to be within the child control hierarchy of a <form runat="server"/> control. The MVC helpers such as Html.RenderPartial do not do anything with the control tree. Ideally you should not mix MVC and WebForms controls. It can work in certain situations but fails in others.

I have the same trouble! just posted a question like that! In my opinion the partial rendered page is not aware of the parent page and that's why you get an int instead of the address of the parent page!
In case you want to generate a Static ClientID you can use this feature in .NET 4! It will be easier to code your JavaScript code knowing that the value won't change!

Related

Getting an element defined in a core:html by its ID

I used this line to define a canvas element in my view.xml:
<core:HTML content="<div class="wrapper col-6"><canvas id="
myChart"width="800"height="400"></canvas></div>">
</core:HTML>
Now I want to get the element in the controller but the typical this.getView().byId("myChart") doesn't seem to work even though the site successfully loads a canvas with the ID.
Is there a way to get those types of elements defined inside a core:HTML tag for the controller?
If not, is there a different way to create a canvas or other HTML elements so that I can refer to them with an ID / use them in the controller?
The element created in this way is not 'registered' in UI5 framework like the other controls. byId() only checks the internal register.
You can use jQuery or standard JavaScript to fetch the element though, like $('#myChart') or document.querySelector('#myChart').
You will find the code for this in Core.js or Core-dbg.js.
Like Jorg said, byId is for retrieving controls. So if you were to put an id on the HTML control, you could retrieve that control and then call getDomRef() on it to get the outermost element, which in your example would be the div. If you'd further only put the canvas inside the HTML control, you'd get that.
If you're accessing the id of the canvas directly, like Jorg suggested, you'll run into trouble if you're going to use the view twice inside a page, because the id of the canvas isn't unique anymore.
There is a third and IMHO preferable option, that is to use html directly inside the view. First you'll need to declare a namespace for it, like
xmlns:html="http://www.w3.org/1999/xhtml"
preferably right on your View element.
Then you can write html directly in your xml view like this:
<html:div class="wrapper col-6">
<html:canvas id="mycanvas" width="800" height="400"></html:canvas>
</html:div>
This way you're getting a proper (unique) id for your canvas and can access it as part of the view's dom with this.getView().getDomRef("-mycanvas"). Note the extra leading dash, because of internal id generation inconsistency in UI5. Also note that getDomRef() is considered protected, but I doubt it will change. Finally, remember that you can only get a domref for rendered controls, so you'll probably want to access it from an afterRendering event.

Render a partial view with sailsjs

I have a div which displays edit button on hover.
But I don't know how display a partial view representing an edit form in this div only instead of the show view.
I haven't no backbone or client-side js framework plugged in my sailsjs app, I didn't manage to understand how it works, not enough documentation about backbone or angular + sails for me.
Could you help me for rendering this partial view please ?
Thanks by advance,
Cyril
Sails uses ejs-locals in its view rendering code, so in your views you can do:
<%- partial ('foo.ejs') %>
to render a partial located at /views/foo.ejs. All of your locals will be sent to the partial automatically.
One thing to note: partials are rendered synchronously, so they will block Sails from serving more requests until they're done loading. We're considering replacing the code for loading partials to make it asynchronous, but for now it's something to keep in mind while developing your app, especially if you anticipate a large number of connections.

Reuse common bits of HTML Across Views in Kendo SPA via MVVM

I have some small bits of HTML that are used in nearly all of my views. I would like to be able to abstract these out of the views so that when I make changes to one view I don't have to update it in everyone of my views. I am not sure how to achieve this. I have tried the following via the MVVM declarative syntax.
<div id="toolbar" class="pull-right" data-role="view" data-template="edit-tool-bar"> </div>
But this doesn't do anything. I do not want to add code to push the HTML into the view if I can avoid this, that is the entire point of MVVM, right? I would be very thankful for any help.
The standard seems to be to use the source binding and specify a template. Since the main view is already bound to a VM telling I set the source binding to this.
<div id="toolbar" class="pull-right" data-bind="source: this" data-template="edit-tool-bar"></div>

seam redirect page

I use seam to manage a viewer page.
In my page I have a div (iframe) where an html page is displayed on particular conditions.
In particular I need to change the page displayed in this div deciding from serverside.
I try to explain me better:
on commandButton click the control is passed to server (using an action). This action method set some things and know what page should be loaded after (or none in some cases).
when the control come back to the caller page, the new page setted by the action method must be called (it uses the things specified by the action method).
How can I do this?
NOTE: the oncomplete tag is not usable in this context because I have many commandButton and it is called everytime I click on one of the commandButtons.
I'm not completely sure what your trying to do.
Are these pages rendered dynamically?
You can generate HTML from the serversite and just output it using a <h:outputText id="iframediv" value="#{controller.htmlparameter}" escape="false" />
An <a4j:commandButton reRender="iframediv"> could then reRender this dynamically generated page.
If the pages are defined statically you could include them like this.
<rich:panel id="iframediv">
<a4j:outputPanel rendered="{controller.page1}">
<ui:include src="page1.xhtml" />
</a4j:outputPanel>
<a4j:outputPanel rendered="{controller.page2}">
<ui:include src="page2.xhtml" />
</a4j:outputPanel>
</rich:panel>
The controller's page1 and page2 parameters should be booleans and set by the action you've called.
Solved using javascript activated only when a particular property is present over the serverside.

How to handle html templates in MVC2?

My (MVC2) application displays several addresses in a View.
Each address contains just a subset of information, like first- and lastname. The request is that the complete address information should be displayed when the mouse is over a result.
An html template should be used therefor.
This template defines how(!) the complete address should be displayed – but it doesn’t defines what(!) should be displayed.
Means it can be assumed that the complete address is always “firstname“, “lastname”, “street”, “zipcode” and “city” (for example and to keep it simple). This will never be changed.
But for example the background color can be changed in the html template from white to green or the size of the lastname can be changed from <h1> to <h2> …
What is the best way to solve it?
I would prefer to write some shared code (keyword: ascx).
This shared code should wrap the template, would be very easy and would look like this:
<div id=”mouseOver” style="display: none;" >
X_REPLACE_X
</div>
The template would look like this simplified example:
FirstName: {Name}<br/>
Lastname: <h1>{Lastname}/<h1><br/>
ZipCode: {ZipCode}<br/>
I would then render the ascx code via “Html.RenderPartial” on the View and map each address to a javascript mouseover function.
The javascript function would replace the placeholder (like {FistName}, {LastName} , etc.) in the template, position and display it.
And thats my problem:
The template should NOT be put directly in the wrapper (ascx - code)!
Means at runtime must “X_REPLACE_X” be replaced with a somewhere on the server stored template.
Because this gives me the ability to change the template without changing and publishing the code!
How can this be managed?
Is there a much better way to solve it? Should I use instead ajax calls to get the template in a variable?
Any help would be really great!
thxs in advance!
It sounds like the best way to accommodate your display differences is to simply use different css classes. The html template used for the full information does not change and can be used for all addresses, while the differences between background colors and text size can be handled by different css settings.
Relatedly, I wouldn't handle the different text sizes by using <h1></h1> and <h2></h2> but rather a common tag (such as <span></span>) whose text size is handled via css.