Jeyhun Rahimov
Jeyhun Rahimov

Reputation: 3787

How to make href link?

I use AD Gallery. but my images is in folder that name equal product create date. Every model's images is in @Model.Date.Value.ToShortDateString() named folder. In this code img src="" works, but href="" does not work:

<div class="ad-thumbs">
  <ul class="ad-thumb-list">
     <li>
       <a href="http://localhost:20234/Products/Images + @Model.Date.Value.ToShortDateString() + @Model.ImagePath1">
          <img src="@Url.Content( Path.Combine( "~/Products/Images/", @Model.Date.Value.ToShortDateString(), @Model.ImagePath1 ) )" width="42" height="42" alt=""/>
        </a>
     </li>
  </ul>
</div>

I also tried

<a href="Path.Combine("http://localhost:20234/Products/Images", @Model.Date.Value.ToShortDateString(), @Model.ImagePath1)">
...
</a>

and

<a href="~/Products/Images" + @Model.Date.Value.ToShortDateString() + @Model.ImagePath1">
...
</a>

In AD Gallery down small images seems, but large images does not opens. How can I write href link to open large images?

Upvotes: 0

Views: 630

Answers (2)

Snake Eyes
Snake Eyes

Reputation: 16764

use Url.Action

<a href="@Url.Action(Path.Combine("http://localhost:20234/Products/Images", @Model.Date.Value.ToShortDateString(), @Model.ImagePath1))">
...
</a>

Also read Razor code between double quotes

Update 1:

@Url.Action(string.Format("http://localhost:20234/Products/Images/{0}/{1}", @Model.Date.Value.ToShortDateString(), @Model.ImagePath1))

From MSDN:

public static string Combine(
    string path1,
    string path2
)

You provided 3 parameters.

or

@Url.Action(Path.Combine("http://localhost:20234/Products/Images", Path.Combine(@Model.Date.Value.ToShortDateString(), @Model.ImagePath1)))

to use Path.Combine

UPDATE 2:

<a href="<%=Url.Action(string.Format("~/Products/Images/{0}/{1}" , @Model.Date.Value.ToShortDateString() , @Model.ImagePath1))%>">
...
</a>

UPDATE 3:

<a href="<%=Url.Content(string.Format("~/Products/Images/{0}/{1}" , @Model.Date.Value.ToShortDateString() , @Model.ImagePath1))%>">
...
</a>

Upvotes: 1

Mihai Labo
Mihai Labo

Reputation: 1082

Why not use Jquery ?

<a href="#" id="addQuick" date="@Model.Date.Value.ToShortDateString()" path="@Model.ImagePath1" class="callmyJquerymethod">name</a> 

jquery:

 $(document).on("click", ".callmyJquerymethod", function () {
   var requrl = '@Url.Action("redirectoAction", "YourController", null, Request.Url.Scheme, null)';
        $.ajax({
            type: "POST",
            url: requrl,
            data: { date: $(this).attr("date"),path:$(this).attr("path") },
            success: function (data) {
               // q.e.d.
            }
        });

In controller :

 public ActionResult redirectoAction(string date, string path)
        {
           string link=string.Format("localhost:20234/Products/Images/{0}/{1},date,path); 
           return Redirect(link);
        }

This should do it ! Use debugger and let me know if it reaches the controller.

Upvotes: 1

Related Questions