Passing parameters to ASCX user control in MVC - asp.net-mvc

I am trying to pass a string parameter to an ASCX. I want to set this to the text property of a label. In the code below it shows betwen the div tags (ignore the % signs in the html tags).
<# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" >
<%asp:Label ID="Label2" runat="server" **Text="<%# Model %> Test"** CssClass="isslabel"><%/asp:Label>
<%div><%: Model %><%/div>
However in the label tag no matter what I put between the angle brackets (bit it bold) in the text tag I cannot get the parameter to appear. I have tried <%: Model %> to no avail. Is the issue that the code block is inside quotes and am I just missing some character?

Why are you trying to use a web forms user control with MVC?
Assuming the model is the string you want to display and you are passing this through correctly, along the lines of
<% Html.RenderPartial("MyUserControlView", "My String To Display"); %>
In your "parent" page, you will be able to do the following in your ascx:
<%= Html.Label(Model) %>
Instead of <asp:label...
Update
If you need to specify then you have a number of options, you could wrap the Html.Label call in a div and specify the class of the div (updating your css accordingly), you could use a display template, or simply explicitly use the Html like the following:
<label for="someIdThatICouldUseAnotherHtmlExtensionMethodToGet"><%: Model %></label>
The key problem with your code (as now also pointed out in the comments by #mare) is that you are trying to use a web forms control in an MVC view.

Related

Blog Title Scraping

I think I might just be over complicating things instead of keeping it simple.
My question is: I want to capture the title of a blog post into a prop variable, and the author who wrote it into another prop variable.
My thought would be to create a page load rule focusing only on the path of /blog. From there I would scrape the page looking for the class that defines it, and then pass it into my prop through DTM.
<div class="field field-name-title">
<h2>Online Education</h2>
<div class="field field-name-body">
<p>
<em> by Author Name</em>
</p>
</div>
</div>
I create a page rule pick my prop and set it as: div.field.field-name-title.innerText But when I set it, all I'm seeing being passed is the "div.field.field-name-title.innerText"
Am I tackling this in the wrong way?
The values you enter in a text field are literal, with the exception of %data_element% syntax, which signifies a reference to a Data Element (there are a couple of other built-in variable references, as well).
Point is, if you want to populate your Adobe Analytics variable from scraping page content, you need to create a Data Element that returns the desired value, and then reference the Data Element in the text field for the Adobe Analytics variable.
That aside, your selector is wrong. What you've done is some weird mix of css selector and javascript syntax.
Below is an example of what you can do, based on your posted HTML:
<div class="field field-name-title">
<h2>Online Education</h2>
<div class="field field-name-body">
<p>
<em> by Author Name</em>
</p>
</div>
</div>
Data Element: Article Title
First, create a Data Element to get the article title from the page, based on your html structure.
Go to Rules > Data Elements > Create New Data Element
Fill out the fields with the following:
Name: article_title
Type: CSS Selector
CSS Selector Chain: div.field-name-title h2
get the value of: text
[X] Scrub whitespace and linebreaks using cleanText
Then, click Save Changes
Data Element: Article Author
Next, create another Data Element to get the article author from the page, based on your html structure.
Go to Rules > Data Elements > Create New Data Element
Fill out the fields with the following:
Name: article_author
Type: CSS Selector
CSS Selector Chain: div.field-name-body em
get the value of: text
[X] Scrub whitespace and linebreaks using cleanText
Then, click Save Changes
Page Load Rule: Populate Variables
Finally, within the various form fields of your Page Load Rule, you can now reference your Data Elements with %data_element_name% syntax.
Tip: Once you start typing the Data Element name out (starting with % prefix), DTM will show an auto-complete dialog, listing Data Elements matched.
If you need to reference the Data Element within a javascript custom code box within the Page Load Rule, you can use the following syntax:
_satellite.getVar('data_element_name');
Where 'data_element_name' is the name of your Data Element.
Example:
s.prop1 = _satellite.getVar('article_title');
Note: Unlike the form field syntax, you should not wrap your Data Element's name with %

MVC - How to render a 'a href' link in a View that's been stored in a database?

Question background:
I have an MVC4 app whos homepage data is supplied from a View Model.
The Issue:
Within the View Model I have a property called AboutUsDescOne This is set from the Controller and Action Method from data that has been retrieved from a database. Within the data set on the property is a a href link, The following shows the text stored in the database:
This is the website Google Google click the link.
The issue I'm having is when I render the data in the View the a href is being seen as text in the View and is not showing as a link, as shown:
The Code:
The propety is simply set as a propety on a standard ViewModel:
public string AboutUsDescOne {set; get;}
This is how it is then rendered in the View:
<div class="col-md-8 form-group">
<p>
<h4>#Model.HomePageVM.AboutUsDescOne</h4>
</p>
</div>
Can anyone tell me how to get the link to render in the View?
Try this:
<h4>#Html.Raw(Model.HomePageVM.AboutUsDescOne)</h4>
Use this helper with caution though. If a user can edit this block of HTML then you might be making yourself vulnerable to an XSS attack:
Text output will generally be HTML encoded. Using Html.Raw allows you to output text containing html elements to the client, and have them still be rendered as such. Should be used with caution, as it exposes you to cross site scripting vulnerabilities.

How to show user control in MVC view

I have my user control in a class library and I have a public method in it which returns the user control as html string. I want to display this HTML in my MVC view. I am using the following code in my view:
<div>
<% MyControlsNamespace.MyControl mvc = new MyControl();
mvc.LoadMyControl(); %>
</div>
LoadMyControl() returns html as string. I can't see any thing in my view when I open it in a browser. I am new to MVC and I knew I am missing some thing here. Any guess??? I am using MVC1
Use <% Html.RenderPartial("Path/To.ascx") %> or <%= Html.Partial("Path/To.ascx") %>.
Use RenderPartial when you're fine with the user-control rendering directly to the response stream, or use Partial if you want to intercept the output as an MvcHtmlString. RenderPartial is faster and generally preferred, only use Partial if you know you need the MvcHtmlString.
More information is available here: Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction

Div tag issue in partial form

I've used div id tag, which used to give me drop down in index.html it worked fine, and when I used it in partial form the div part is not working.
I've used JavaScript and Ajax as well.
The code is like this..
<div title="Show More" class="show_more"></div>
If the issue is after adding a partial it is possible your code needs the addition of
locals:
or
collection:
See this post for more info on how to Pass a variable into a partial, rails 3?
the javascript was not being called, so I had to use it again in my partial form and it worked.

Nesting HTML <span> inside rails form_for label tag?

I want to nest a element inside a form_for label tag. I want to do this so I can target a specific portion of the label with CSS rules, in this case to make the text red. From some quick reading, this does appear to be valid HTML, and it fits with my design even though the idea is not playing happily with Rails.
The desired html output is like this:
<label for="zip">ZIP Code -<span class="required">Required</span></label>
My current code looks like this:
<%= form.label :zip, 'ZIP Code -<span class="required">Required</span>' %>
The problem is that Rails is somehow escaping the inner span tag so that it appears as text on the page instead of HTML. I see this on the page:
ZIP Code -<span class="required">Required</span>
Rails3 automatically escapes strings. You need to call #html_safe on the string you're putting in the label. See http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/ for details.

Resources