Displaying HTML from a Database by using MVC 3 (ASPX ViewModel) - asp.net-mvc

I'm using MVC 3 (the ASPX ViewModel) while I store and display data from my SQL database. I've tried using the raw input to store it as well as using HttpUtility.HtmlEncode. Neither are working when I try to display. I've tried using the HttpUtility.HtmlDecode as well as using <%: Model.MyHtmlVariable %>. Am I missing something?

Using the traditional "<%= html %>" syntax should render it out for you but may not depending on what you're doing. If not, try to wrap it in an HtmlString object, like so:
<%= new HtmlString(html) %>
MVC should respect that and render it out properly.
If you're just looking to display the encoded HTML, the "<%: html %>" syntax is your friend

You need to create a div to target and set the html using an jquery/javascript call to the controller action.
jQuery.get("/Controller/Action",
function(response) {
$("#MyDiv").html(response)
});
See if something like that works.

Related

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.

Html.Partial or Html.RenderPartial with MVC3?

I am totally confused even after seeing the following explanation.
<div>
#Html.Partial("_FeaturedProduct")
</div>
Partial views can be rendered inside a Layout Page (or if using MVC 2/3 w/ASPX, the Master Page) as well as regular views.
There are some cases where you might like to step aside and write directly to the HTTP Response stream rather than having a partial view render the results (partials/views use MvcHtmlString/StringWriter). To do so, use the Html.RenderPartial helper.
<div>
#Html.RenderPartial("_FeaturedProduct")
</div>
Can someone tell me what it means? What cases where I might like to write direct to the HTTP Response etc. What if my partial view contains just one line like this:
<h1>Hello</h1>
Which should I use and why? What would happen if I used the other?
The following confused me even more: "Use Html.RenderPartial for streaming images or other elements that are media-centric or where faster download times are highly important."
See the response below.
The only difference is that Partial returns an MvcHtmlString, and must
be called inside <%= %>, whereas RenderPartial returnsvoid and renders
directly to the view.
If you look at the source code, you'll see that they both call the
same internal method, passing a StringWriter for it to render to.
You would call Partial if you want to view, save, or manipulate the
generated HTML instead of writing it to the page.
What is the difference (if any) between Html.Partial(view, model) and Html.RenderPartial(view,model) in MVC2?

Building pagination in MVC: Is there anyway to get something like an Html.HyperLinkFor

What I mean by that is a type element that will have a label based on a value in my viewModel and also be able to submit that value back up to the viewModel so it can grab new results based on Current Page and Page Size. This is for creating a gridview in MVC that supports pagination.
None of the examples I've seen of MVC so far have had anything resembling a gridview. It's important that I create my own paging and not use any built in paging mechanisms or third party controls or html helpers or the like
I'd go with ActionLink. To build Urls you can either use Url.Action or Html.BuildUrlFromExpression(c => c.conTrollerAction)
If you use T4MVC, you will get some nice helpers that do exactly what you are looking for.
Something along the lines of:
<a href="<%: Url.Action(MVC.MyController.MyAction(Model.ActionMethodParam1, Model.ActionMethodParam2)) %>">
You can also use the Html.ActionLink helper:
<%: Html.ActionLink("Link Name", MVC.MyController.MyAction(Model.ActionMethodParam1, Model.ActionMethodParam2)) %>
T4MVC is a great little library that I use in every MVC app.

Resources