Problem with displaying image from DB using asp.net MVC 2 - asp.net

I'm having trouble with displaying image from db. I think that method for saving image is working, because I see in DB that varbinary fields have some value, and that image type is correct. Also the size of image. But when I want to display image for product i don't get anything. Just blank space.. here is my code...
public byte[] GetImage(int productID)
{
Product product = products.GetByID(productID);
byte[] imageData = product.ImageData.ToArray();
return imageData;
}
That code is in my controller. Second is code from view :
<% if (product.ImageData != null) { %>
<img src='/product/getimage/<%= Html.Encode(product.ID) %>' alt=""/>
<% } %>
I tried some solutions found here on stack overflow, and everyone do it like this, but it's working for them. I dont have any idea why images aren't displayed. When i look at source code of page at debugging i have :
<img src='/product/getimage/18' alt=""/>
I'm using .net 4.0, MVC 2, VS 2010... Thanks in advance

My guess is that you have a routing issue. If the url /product/getimage/18 should work for your action you need to have a route that looks something like this:
routes.MapRoute("ProductImage",
"product/getimage/{productID}",
new { controller = "Product", action = "GetImage", productID = "" }
);
To confirm this you should use firebug and check the "net" tab (or set a breakpoint in your action and see if it gets hit when you load the page).
I would also recommend using the build in helpers to generate your urls (then this kind of problem will never happen). Instead of src='/product/getimage/<%= Html.Encode(product.ID) %>' you can write src='<%=Url.Action("GetImage", "Product", new { productID = product.ID })%>'

I'm not much of an expert on MVC yet - but from what I can see, I think your GetImage function needs to be an Action Method inside your controller, so it'll need to return an ActionResult?
You'll probably need to set the Content-Type header in the response, too.
This guy looks like he knows what he's talking about...
http://blogs.msdn.com/miah/archive/2008/11/13/extending-mvc-returning-an-image-from-a-controller-action.aspx

Try to change you method to:
public void GetImage(int productID)
{
Product product = products.GetByID(productID);
Response.ContentType = "image/jpeg";
Response.BinaryWrite(product.ImageData.ToArray());
}

You have:
<img src='/product/getimage/18' alt=""/>
Normally i'd say an image tag looks like this:
<img src='/product/getimage/18.jpg' alt=""/>

I needed to register routes in my global.cs file like Mathias suggested.
Also i needed to modify action in my controller to this :
public FileContentResult GetImage(int productID)
{
Product product = products.GetByID(productID);
return File(product.ImageData.ToArray(), product.ImageMimeType);
}
I wish thank you all for your fast answers...

Related

Iframe shows ActionMethod name while loading PDF file in ASP MVC

i am using iframe tag in order to call ActionMethod that returns FileResult to display PDF file. The issue is after it loads PDF document instead of showing pdf file name, it shows ActionMethod name on the top of the PDF file name in Chrome.
Razor Code:
<iframe src="#Url.Action("GetAgreementToReview", "Employee")" style="zoom: 0.60; -ms-zoom: 1; width: 100%;" width="99.6%" height="420" frameborder="0" id="agreementPdf"></iframe>
CS Code:
public ActionResult GetAgreementToReview()
{
Response.AddHeader("Content-Disposition", "inline; filename=Master-Agreement.pdf");
return File("~/Content/Master-Agreement.pdf", "application/pdf");
}
Image: As you can see in the screenshot, it shows 'GetAgreementToReview' i.e. ActionMethod name instead of 'Master-Agreement.pdf'.
Does anyone know how to fix this issue?
Thanks.
Sanjeev
I had a similar problem and found a solution after exploring almost everything the web has to offer.
You must use a custom route for your action and you must send the filename from UI into the URL itself. (Other parameters won't be affected)
//add a route property
[Route("ControllerName/GetAgreementToReview/{fileName?}")]
public ActionResult GetAgreementToReview(string fileName, int exampleParameter)
{
byte[] fileData = null; //whatever data you have or a file loaded from disk
int a = exampleParameter; // should not be affected
string resultFileName = string.format("{0}.pdf", fileName); // you can modify this to your desire
Response.AppendHeader("Content-Disposition", "inline; filename=" + resultFileName);
return File(fileData, "application/pdf"); //or use another mime type
}
And on client-side, you can use whatever system to reach the URL.
It's a bit hacky, but it works.
If you have an Iframe on HTML to display the PDF (the issue I had), it could look like this:
<iframe src='/ControllerName/GetAgreementToReview/my file?exampleParameter=5' />
And this way you have a dynamic way of manipulate the filename shown by the IFRAME that only uses the last part of an URL for its name shown on the web page (quite annoying). The downloaded filename will have its name given from server-side Content-disposition.
Hope it helps !
please try this code :
return File("~/Content/Master-Agreement.pdf", "application/pdf", "filename.pdf");
Other Helper Link : Stream file using ASP.NET MVC FileContentResult in a browser with a name?

Using translation strings in componentlinks

I've been trying to solve this problem since this morning, and I know I'm missing something obvious here but I can't seem to find it.
We are using an XML file that is published to the server which contains translations of all the standard words, such as 'read more'. It is a page with a component that is localized in the appropriate publication.
In our Razor templates we use the following code below a plain news Summary item which in turn links to the full item.
<a tridion:href="#news.ID" class="more" ><%=DefaultLabels.TranslatedTerm(((HomePage)Page).Location, "read_more")%></a>
Thing is, the server tag works just fine, but gets resolved as
<tridion:ComponentLink runat="server" PageURI="tcm:15-407-64" ComponentURI="tcm:15-1475" TemplateURI="tcm:0-0-0" AddAnchor="false" LinkText="<%= DefaultLabels.TranslatedTerm(((HomePage)Page).Location, "read_more") %>" LinkAttributes=" class="more"" TextOnFail="true"/>
As you might notice, it gets written as plain text on the page (no surprise there because the LinkText parameter is declared as String in the first place according to the liveDocs).
If I take away the
tridion:href
in the first example, and write it as
href
It works fine, the code resolves into a translated string and it's even linked... to nothing more than the TCM ID of the component, not the right page with the full news item on it.
I've tried creating a function in Razor, tried replacing the linkText, tried setting the ComponentLink in the template itself, but to no avail. I feel that it should work with just a minor adjustment to this template's code, but I fail to see it and I've started looking at custom TBB's to handle the code.
Anyone have an idea how to resolve this?
EDIT:
Chris' answer was actually the one I was looking for in this particular situation, but I feel that I should point out that Priyank's function is something that should be regarded as such as well. So thank you both for the help, it made my life somewhat easier now!
I hope this razor function will help you lot. This is very helpful function to render the link tag from the component link or external link.
#helper RenderLink(
dynamic link, // the link to render. Handles components + internal / external links
string cssClass = null, // optional custom CSS class
string title = null // optional link text (default is the title of the component being linked to)
)
{
if(link == null)
{
return;
}
if (title == null)
{
title = link.title;
}
string classAttr = string.IsNullOrEmpty(cssClass) ? "" : " class='" + cssClass + "'";
dynamic href;
string tridionLink = "";
string targetAttr = "";
if (link.Schema.Title == "External Link")
{
href = link.link;
}
else if (link.Schema.Title == "Internal Link")
{
href = link.link;
tridionLink = "tridion:";
}
else
{
href = link;
tridionLink = "tridion:";
}
if(link.target != null)
{
targetAttr = link.target == "New window" || link.target == "Popup" ? " target='_blank'" : "";
}
<a #(tridionLink)href="#href"#classAttr#targetAttr>#title</a>
}
I would suggest not using the default templates for resolving your links, rather output the link yourself something like this:
<tridion:ComponentLink runat="server" PageURI="tcm:15-407-64"
ComponentURI="tcm:15-1475" TemplateURI="tcm:0-0-0"
AddAnchor="false" LinkAttributes=" class="more""
TextOnFail="true">
<%=DefaultLabels.TranslatedTerm(((HomePage)Page).Location, "read_more") %>
</tridionComponentLink>
Better still you might consider outputting TCDL rather than the Taglib/ServerControl

render a string in MVC just like rendering a view, possible?

is it possible to render a string like this:
public ActionResult Do(){
s = " hello, click here <%=Html.ActionLink(\"index\",\"index\") %> ";
return Content(RenderString(s));
}
the result would something like this:
hello, click here index
What is the purpose of this? You have a controller action which tries to evaluate some string WebForms syntax string and return it as content. Why not simply return the view and have this view do the job?
If you want dynamic to have views (coming from a database or something) you could write a custom view engine and personalize their location so that your action looks like this:
public ActionResult Do()
{
return View();
}
and the corresponding view contents will be fetched from your custom view engine instead of the standard file locations.
If you want to render the contents of a view into a string this has been covered in many blog posts. Finally if you are dealing with sending views as emails there are probably better solutions.
So depending on what you are trying to achieve there might be different solutions.
public String Do(){
string s = " hello, click <a href='" + Url.Action("Index") +"' > here </a>";
return s;
}
then if you call {Controller}/Do you will have your string
EDITED
Marco

ASP.NET MVC2: modifying master css property depending on query string parameter

I am migrating a web site to a new one using ASP .NET MVC2.
In the original site, master page has code-behind to check a query string parameter value. Depending on this value, code-behind dynamically modify some CSS property to hide / display master page elements.
As MVC2 has no code-behind because we are supposed to perform everything in the controllers, how should I proceed in this case ?
I see this : asp.net mvc modifying master file from a view
It partially answers my needs but the query string processing is common to all pages. How can I move this processing in a common code section ?
Regards.
A helper method looks like a good place:
public static class HtmlHelperExtensions
{
public static string GetCss(this HtmlHelper htmlHelper)
{
// read some request parameter
// here you also have access to route data so the
// parameter could be part of your custom routes as well
var foo = htmlHelper.ViewContext.HttpContext.Request["foo"];
// based on the value of this parameter
// return the appropriate CSS class
return (foo == "bar") ? "barClass" : "fooClass";
}
}
And somewhere in your master page:
<body class="<%= Html.GetCss() %>">
Or if you are always going to apply it to the body tag only it might be more appropriate to do this in order to reduce the tag soup:
public static class HtmlHelperExtensions
{
public static MvcHtmlString StartBody(this HtmlHelper htmlHelper)
{
var body = new TagBuilder("body");
var foo = htmlHelper.ViewContext.HttpContext.Request["foo"];
var bodyClass = (foo == "bar") ? "barClass" : "fooClass";
body.AddCssClass(bodyClass);
return MvcHtmlString.Create(body.ToString(TagRenderMode.StartTag));
}
}
and in your master page at the place of the body tag:
<%= Html.StartBody() %>
I can think of two solutions to this:
Derive your controllers from one controller base and set the ViewData parameter there depending on posted Form values
Don't use ViewData at all, but simply look for the form value in the view (using HttpContext.Current)
The second method violates the MVC pattern. IMO it is still acceptable in some scenarios, for example I am using this approach to highlight the currently selected item in a navigation menu.

ASP.NET MVC 2 - Simple Search Page

I just started ASP.NET MVC (coming from WebForms) and I'm struggling with some really basic concepts.
I want to create a single page that uses a textbox for date input. I would like the date input to be passed to the index of my controller which looks like this:
public ActionResult Index(int month,
int day,
int year){
var visitors = visitorRepoistory.FindVisitorsByDate(month, day, year).ToList();
return View("Index", visitors);
}
Up to this point I have used scaffolding with strongly typed views so everything was kind of glued together for me.
What would/should my view look like? Would I use an actionlink (this is a get request after all, right?) and not a submit button.
Thanks.
I thought about this for a while before trying to come up with an answer. What threw me initially was the concept of turning a single text input string into it's month, day, and year components. In ASP.NET MVC, it would be much easier to just accept the string for the date. This way, your code changes to:
public ActionResult Index(string date) {
try
{
DateTime dtDate = DateTime.Parse(date);
var visitors = visitorRepoistory.FindVisitorsByDate(dtDate.month,
dtDate.day, dtDate.year).ToList();
return View("Index", visitors);
}
catch (FormatException)
{
//String was not a valid date/time
}
}
Are there ways to split it up into 3 ints? I'm sure. But to me, this would be the easiest/quickest way to the goal.
So in the view, you'll have your form looking something like this:
<% using(Html.BeginForm("VisitorSearchController", "Index")) { %>
Enter a date: <%= Html.TextBox("date") %>
<input type='submit' value='Search' />
<% } %>
Where "VisitorSearchController" is the name of the controller you want to post back to. Of course, "Index" is the method you're posting to. I'd stick with the submit button for now unless you're trying to get a LinkButton equivalent on the page. But you can save the "prettying up" part after functionality, right?
Edit: Added view code to the answer.
EDIT after comment:
Simplest way is to make the search input page post (not get) back to some other method, parse the date out, then redirect to the Action you have specified.
If you want to do it through get, then you can use some Javascript trickeration to link to whatever they type in, but I recommend the former.

Resources