How to Translate this Image Source into Url.Content? - c#

I have the following image which is rendered this way.
<img src="../../../..#Model.FloorPlan.Floor_Plan_Image_Path#Model.FloorPlan.Floor_Plan_Image_Filename" alt=""/>
I want if possible it's src attribute will be changed into Url.Content.
What I have tried is this but my problem is it treats my model as string:
<img src="#Url.Content("~/Model.FloorPlan.Floor_Plan_Image_Path#Model.FloorPlan.Floor_Plan_Image_Filename")" alt=""/>
Can anyone help me?
The value of the Path and Filename is as follows:
Model.FloorPlan.Floor_Plan_Image_Path = "/Content/Uploads/FloorPlans/00004601/"
Model.FloorPlan.Floor_Plan_Image_Filename = "testfloorplan.png"

I found myself in a situation where I would need to format the string almost on any view,
so I made an extension method for this, just to get rid of those String.Format's on every View.
public static class UrlHelpers
{
public static string Content(this UrlHelper urlHelper,
string formatPath,
params object[] args)
{
return urlHelper.Content(String.Format(formatPath, args));
}
}
It just simply formats the specified path, nothing much going on, however calling it would be a bit nicer to read.
#{
var path = Model.FloorPlan.Floor_Plan_Image_Path;
var imageName = Model.FloorPlan.Floor_Plan_Image_Filename;
}
<img src="#Url.Content("~{0}{1}", path, imageName)"/>

I think I understand what you're going for. Try this:
<img src="#Url.Content(String.Format("~{0}{1}", Model.FloorPlan.Floor_Plan_Image_Path, Model.FloorPlan.Floor_Plan_Image_Filename))" alt=""/>

Try
<img src="#Url.Content("~/" + Model.FloorPlan.Floor_Plan_Image_Path + Model.FloorPlan.Floor_Plan_Image_Filename)" alt="" />

Related

MVC - Variable in .cshtml file which updates the html when the controller calls it

this is probably a pretty basic thing for you guys, but I still can't figure it out.
Let's say I have a View:
myView.cshtml
<div>
<p>Some content here</p>
#MyHTMLVariable //Calls updateHTML -> MyHTMLVariable = s
<p>Some more content over here</p>
</div>
myController.cs
public string updateHTML()
{
s = "I'm a string"; //Changes dynamically, handled by different stuff outside
//Any string to html-conversion needen?
return s;
}
How can I "update" the variable in the view / how do I have to initialize it?
Cheers,
DDerTyp
Use Razor's #functions code block to declare your functions within your view this way...
#functions {
public string updateHtml()
{
string s = "I'm a string";/*or whatever dynamic stuff you wanna do*/
return s;
}
}
#{
string myHtmlVariable = updateHtml();
}
Then use #myHtmlVariable within your code.
You can use the ViewBag for this. In your template use #ViewBag["MyHTMLVaribale"] and in your controller method use ViewBag["MyHTMLVariable"] = "I'm a string";

Addressing an images in View with its local path

How to display this image in an ASP.NET MVC project?
I've tried this, but it wouldn't work:
<img src="C:\BBBB\1.png" />
Any brilliant suggestion, please?
Thanks a lot!
I did something similar to this to display payroll stubs. I didn't want them accessible from within wwwroot.
See this article for serving an image from an mvc action.
ASP.NET MVC3: Image loading through controller
public ActionResult ShowImage(id)
{
var file = #"C:\BBB\"+id;
var ext = file.Split('.').Last();
return File(file, "image/"+ext, Path.GetFileName(file));
}
Call it like this:
<img src="#Url.Action("ShowImage",new { id="1.png" })" alt="1.png"/>
For a loop, you could do something like this in your existing view:
for (var i = 1; i < 10; i++ ) {
#Html.Partial("ShowIfExists", new {id=i.ToString()+".png" })
}
Create a new Razor View called _ShowIfExists.cshtml
<img src="#Url.Action("ShowImage",new { id=Model })" alt="1.png"/>
And add a corresponding action:
public ActionResult ShowIfExists(id)
if ( System.IO.File.Exists(#"C:\BBB\"+id) )
return PartialView("_ShowIfExists",null,id);
}
return new EmptyResult();
}
The image tag's src needs to point to where the image resides on a web server.
<img src="http://myserver.com/myimage.png" />
You need to reference images with relative address like this: ../images/1.png.
check this tutorial to start: W3schools Html Tutorial/img Tag
If you insist on displaying a local image, you should include the protocol, which in this case is file://:
<img src="file://C:/BBBB/1.png" />
This is not a good idea for real projects tho.

Displaying image from db & OutputStream Exceptions ASP.NET/C#

Good night-morning-evening-etc ^_^
I'm getting problems while trying to display an image in asp:Image (using Web Forms), stored in a db as byte[] - found a lot of rather clear answers, how to do it, so now I have a handler:
public class ShowImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//creating object, working with db
var core = new CoreHolder();
var picture = core.PictureRepository.Read(Convert.ToInt32(context.Request.QueryString["id"]))
context.Response.Clear();
context.Response.ContentType = picture.PictureMimeType;
//trying to write byte[]
context.Response.BinaryWrite(picture.PictureData);
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
...and such strings in my .aspx page:
<asp:Image ID="Image1" runat="server" ImageUrl="~/ShowImageHandler.ashx?id=<%#:Item.ID %>" />
<asp:Image ID="Image1" runat="server" ImageUrl="~/ShowImageHandler.ashx?id=1>" />
The problems are: ok, the program enters the ProcessRequest with id, and in case of the second asp-image string it finds the pic with data, but even before trying to BinaryWrite I can see, that there are exeptions in context.Response.OutputStream: length,position - System.NotSupportedExeption, Read/WriteTimeout - System.InvalidOperationExeption. In case of the first string (it's used in ListView ItemTemplate) the OutputStream problem stays + all crushes on trying to get the id from the query string.
Help, please)
The errors you see in the debugger for context.Response.OutputStream.Length and Position don't matter. You can only write to that stream so the exceptions you see in the debugger display are expected.
Your code looks fine so my guess is that if you look at the URL, the value for your id querystring argument will not be an integer. You are probably getting a FormatException when you try to convert it to an integer.
You should test the handler by putting the url "ShowImageHandler.ashx?id=1" in your browser's address bar instead of using an <img /> tag. That way if there is an exception you can get a real stack trace instead of just seeing a broken image.

run a method on a Databinder.Eval object and convert to a string

I have a repeater that outputs an image path.
I am trying to check if the file exists.
I am having issues with the method in the cs
public string doesImageExist(object sysid)
{
string mySysid = sysid.ToString();
string curFile = "C:\\inetpub\\wwwroot\\xxx\\xxx.com\\listings\\Photo" + mySysid + "-1.jpeg";
if (File.Exists(curFile))
{
return "http://www.xxxx.com/listings/Photo" + sysid + "-1.jpeg";
}
else
return "http://www.xxxx.com/images/multiple-listings.jpg"; // placeholder file
}
on the page I am using this code
<img class='img-rounded'
src='<%# doesImageExist(Eval(Container.DataItem, "SYSID"))%>'
alt='' style='float:left; width :350px; margin: 10px;' />
I have tried changing the method to accept a string and then add "as String" to the eval and it still did not work...
Thanks for any advice on this
Try using overload of Eval and call ToString() to pass string to server side method.
src='<%# doesImageExist(Eval("SYSID").ToString())%>'
public string doesImageExist(string sysid)
{
//your code
}
You can use ToString()
<img class='img-rounded'
src='<%# doesImageExist(Eval(Container.DataItem, "SYSID").ToString())%>'
alt='' style='float:left; width :350px; margin: 10px;' />
and then change the method back to string as the param
public string doesImageExist(string sysid)

httphandler return into string variable

I have an httphandler that returns a string (a part description).
I'd like to use the return value in a title attribute on an image, which will be updated on a page load.
I have the following code in page_load event...
imgThumbnail.Attributes.Add("title", "~/Views/Admin/ImageRepository/ShowDescription.ashx?partno=123456&view=thumb");
I'm not sure how to get the title text to be the return value instead of just "~/Views/Admin/ImageRepository/ShowDescription.ashx?partno=123456&view=thumb".
How can I do this? This is the first time using an http handler. I have tested the handler and it does return the string I expect it to.
I think you might have to rethink how you are approaching this, your code might look like this:
string imgTitle = "Image Title"; // you need to set the value of imgTitle here.
imgThumbnail.Attributes.Add("title", imgTitle);
This might give an output like this:
<img title="Image Title" />
You probably don't want a handler for this, essentially, you could move the code from your handler to your page.
string imgTitle = GetImageTitle(partno); // you need to set the value of imgTitle here.
imgThumbnail.Attributes.Add("title", imgTitle);
public string GetImageTitle(string partno)
{
// put code to return image title here;
}
PS are you sure you don't want the alt attribute, <img> doesn't have a title attribute.

Categories

Resources