我的控制器返回一个字符串,该字符串是外部网站图像的网址。 如何在视图上显示该URL。 感谢您的帮助。
答案 0 :(得分:2)
AngryHacker是正确的。我只是用一些代码示例来扩展AngryHacker的答案。
在ViewModel中为图片网址添加一个属性,并在第一个get调用中返回该属性。然后在视图中使用它。因此,您正在避免再次对该操作发出不必要的http请求
public class UserProfileViewModel
{
public string DisplayName { set;get;}
public string GravatarURL { set;get;}
}
并在您的ACtionMethod中
public ActionResult Get(int id)
{
UserProfileViewModel objVm=new UserProfileViewModel();
objVM.GravatarURL="http://www.externalsite.com/image/tiyra.jog";
//Set other properties also.
return View(objVm);
}
并且在强类型为UserProfileViewModel的视图中
@model UserProfileViewModel
<h2>"@Model.DisplayName </h2>
<img src="@Model.GravatarURL" />
<p>The image is loaded from @Model.GravatarURL</p>
答案 1 :(得分:0)
将URL作为模型的一部分,并在视图中引用它。
答案 2 :(得分:0)
也许您错过了必须使用&lt;%:%&gt;对输出进行HTML编码的部分。标签,la:
<%: Html.Label(ViewData["PicUrl"].ToString()) %>
...或者,如果它是你模特的字符串属性......
<label><%: Model.PicUrl %></label>