首页 > c# > 如何使用模型中的值设置tag属性

如何使用模型中的值设置tag属性 (How to set tag attribute with value from model)

问题

我在视图中有img标签,我想用模型对象的值设置该img的src属性:

    @model myObject

    @{
        ViewBag.Title = "title";
    }  

   <img src="@Model.mysrc" alt="error"  />

但它不起作用,我尝试使用jquery但不工作:

$('img').attr("src", "@Model.mysrc");

解决方法

问题是因为@ Model.mysrc =“〜/ images / photo.jpg”中的字符串

它是波浪形的,我删除波浪线,它工作正常,

问题

I have img tag in the view and I want to set the src attribute of that img with value from model object like that:

    @model myObject

    @{
        ViewBag.Title = "title";
    }  

   <img src="@Model.mysrc" alt="error"  />

but it's not work and I try to use jquery but not work too:

$('img').attr("src", "@Model.mysrc");

解决方法

the problem was because of the string in the @Model.mysrc = "~/images/photo.jpg"

it is tilde, i remove tilde and it work right,

相似信息