使用@ url.content方法在MVC 4 Javascript中获取动态图像URL

时间:2014-04-15 11:05:37

标签: jquery asp.net-mvc asp.net-mvc-4

以下是我的情况。

  1. 有一个与NewsVM的IEnumerable强烈绑定的View。

    @model IEnumerable<ProjectName.Models.News.NewsVM>
    
  2. 以下是视图型号名称NewsVM.cs。

    public long news_sk { get; set; }
    public Nullable<int> user_fk { get; set; }
    public string title { get; set; }
    public string short_desc { get; set; }
    public string full_desc { get; set; }
    public string source_link { get; set; } //This can be either local content path or online url
    public string type { get; set; }
    public Nullable<System.DateTime> created_time { get; set; }
    public Nullable<bool> isactive { get; set; }
    public int SectionID { get; set; }
    public string UserImage { get; set; }
    public string UserName { get; set; }
    //Method which will fetch data from DB and populate in this VM and gererate IList<NewsVM> and return it to controller action.
    public static IList<NewsVM> GetNews(int LastID = 0){......}
    
  3. 这是控制器动作方法。

    public JsonResult GetNextNewsContent(int ID)
    {
        IList<NewsVM> lstNewsContentBM = NewsVM.GetNews(ID);
        return Json(lstNewsContentBM, JsonRequestBehavior.AllowGet);
    }
    
  4. 这是视图。我使用$ .ajax方法从Jquery代码调用GetNextNewsContent并接收Json响应。

    <script type="text/javascript">
    $(window).scroll(function () {
    if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
        //alert("End Of The Page");
        //alert($('#LastContentID').val());
        GetNextContents(($('#LastContentID').val()))
    }
    });
    
    function GetNextContents(LastID) {
    debugger;
    var ID = LastID;
    $.ajax({
        type: "POST",
        url: 'News/GetNextNewsContent/' + ID,
        success: function (item, status) {
            debugger;
            var appenstring = "";
            for (var i = 0; i < item.length; i++) {
             appenstring =  "<div class='NewsContainer'>"; 
             appenstring += "<div class='NewsContainer-Userdeails'>";
             appenstring += "<div id='' class='NewsContainer-UserImage'>"
                    if (item[i].UserImage != null && item[i].UserImage.length > 0)
                    {
                        appenstring += "<img src='http://test.clearcellgroup.co.uk:82/Images/Members/'"+ item[i].UserImage + "alt='Image Not found' height='70px' width='50px'/>";
                    }
                    else
                    {
                        appenstring += "<img src='@Url.Content("~/Content/Images/BlankUserImg-small.png")' alt='Image Not found' height='70px' width='50px' />";
                    }
                appenstring += "</div>";
                appenstring += "<div class='NewsContainer-UserName'>"+item[i].UserName + "</div>";
    
            appenstring += "</div>";
            appenstring += "<table style='clear: both;'><tr>";
                    appenstring += "<td valign='top'>";
                        appenstring += "<div class='NewsContainer-Header'>"+ item[i].title + "</div>";
                        appenstring += "<div class='NewsContainer-desc'> <p>"+ item[i].short_desc + "</p>";
                        appenstring += "</div>";
                    appenstring += "</td>";
                appenstring += "</tr>";
                appenstring +="<tr>";
                    appenstring +="<td>";
                        if (item[i].type.toLowerCase() == "video")
                        {
                            appenstring +="<div class='ShadowBorder NewsContainer-div-iframe'>";
                            appenstring +="<iframe class='NewsContainer-iframe' scrolling='no' src='" + item[i].source_link +"'></iframe> </div>";
    
                        }
                        else if (item[i].type.toLowerCase() == "image")
                        {
                            appenstring +="<div class='ShadowBorder NewsContainer-div-iframe'>";
                            **appenstring +="<img src='@Url.Content(item[i].source_link)'** /></div>";
                        }
                        else if (item[i].type.toLowerCase() == "link")
                        {
                            appenstring +="<div class='ShadowBorder NewsContainer-div-iframe'>";
                            appenstring +="<a href='" + item[i].source_link+ "' target='_blank'>" + item[i].source_link +"</a></div>";
                        }
                        else
                        {
                            appenstring +="<div class='ShadowBorder NewsContainer-div-iframe'><span>No Content</span></div>";                                    
    
                        }
                    appenstring +="</td>";
                appenstring +="</tr>";
            appenstring +="</table>";
        appenstring +="</div>";
        debugger;
                if (item[i].SectionID == 1)
                {
                    $('#Section1').append(appenstring);
                }
                else if (item[i].SectionID == 2)
                {
                   $('#Section2').append(appenstring);         
                }
                else if (item[i].SectionID == 0)
                {
                   $('#Section0').append(appenstring);       
                }
                else
                {
                    //appenstring +="<div class='ShadowBorder NewsContainer-div-iframe'><span>No Content</span></div>";                                    
                }
            }
        },
        error: function (error, status) {
            alert("error");
        }
    
    });
    }
    

    我想加载服务器的图像文件夹中的图像。如何在Jquery代码中获取服务器文件夹的路径。我试过这个'@Url.Content(item[i].source_link)'但是没有用,因为item[i].source_link在编译时不可用,@Url.Content在编译时得到解决。

  5. 那么有没有其他方法可以达到同样的效果.. ??? 任何帮助将不胜感激。 提前谢谢..

0 个答案:

没有答案