Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法在html属性中连接字符串?_Html_Asp.net Mvc 3 - Fatal编程技术网

有没有办法在html属性中连接字符串?

有没有办法在html属性中连接字符串?,html,asp.net-mvc-3,Html,Asp.net Mvc 3,我正在使用MVC3,我想使用局部视图来创建动态DOM元素。这是我目前的部分观点: @model MVCApp.ViewModels.TitlesViewModel <div class="display-label">Name</div> <div id="label"+"@Model.Id" class="display-field">@Model.InitValue</div> 因此,如果我尝试做一些类似的事情: alert($("#lab

我正在使用MVC3,我想使用局部视图来创建动态DOM元素。这是我目前的部分观点:

@model MVCApp.ViewModels.TitlesViewModel

<div class="display-label">Name</div>
<div id="label"+"@Model.Id" class="display-field">@Model.InitValue</div>
因此,如果我尝试做一些类似的事情:

alert($("#label1").text())
有一个警报框,里面什么都没有

那么,如何将这两个字符串加在一起,形成一个由jQuery(或document.getElementByID(str))识别的连贯字符串呢

<div id="label@Model.Id" ...
试试这个(已验证):

@Model.InitValue

只需添加另一个选项,因为这是我在@html.ActionLink中尝试将字符串和模型值转换为id以及文本值时的工作方式。我需要使用string.Concat。不知道从性能角度看这是否糟糕

  @Html.ActionLink(string.Concat("View all (", @Model.FooCount, ")"),
        //actionName        
        "SeeAllFoos",
        //ControllerName
        "Foo",
        // routeValues
        new { FooId = @Model.Foo.id },
        //htmlAttributes
        new { @class = "btn btn-success", onclick = "ShowProgress();",
        id = string.Concat("Foo",@Model.Foo.id.ToString()) })

这是你需要做的

id="label_@Model.Id"

下划线(\)是必需的。对我来说,在没有下划线的情况下传递id是一个问题。祝你愉快。

我试过了。在我的浏览器的HTML源代码中,id仅为“label@Model.Id“这很有效:
@Model.InitValue
非常感谢。刚才添加了引号(当你发表评论时):)是的,我看到了。不过,谢谢你的帮助。我对MVC3和Razor语法还是新手。
<div id="@("label"+Model.Id)" class="display-field">@Model.InitValue</div>
  @Html.ActionLink(string.Concat("View all (", @Model.FooCount, ")"),
        //actionName        
        "SeeAllFoos",
        //ControllerName
        "Foo",
        // routeValues
        new { FooId = @Model.Foo.id },
        //htmlAttributes
        new { @class = "btn btn-success", onclick = "ShowProgress();",
        id = string.Concat("Foo",@Model.Foo.id.ToString()) })
id="label_@Model.Id"