将 Html.DropDownListFor() 与 jQuery 结合使用(添加包含下拉列表的另一行)

发布于 2024-11-07 05:43:51 字数 2451 浏览 0 评论 0原文

我在使用 Html.DropDownListFor() 和 jQuery 时遇到问题。我正在使用 ASP.NET MVC 2、C#、.NET 4 和 jQuery 1.4.2。

我收到的错误是“Uncaught SyntaxError: Unexpected token ILLEGAL”(使用 Chrome 的开发人员工具在控制台中看到此错误)。

我将解决这个问题:

这是控制器中我的 SelectList 的设置:

            var Additives = new SelectList(
            new[]
            {
                new { Value = 1, Name = "FCS" },
                new { Value = 2, Name = "Fungizone" },
                new { Value = 3, Name = "HEPES" },
                new { Value = 4, Name = "Investigator supplied" },
                new { Value = 5, Name = "L-Glutamine" },
                new { Value = 6, Name = "None" },
                new { Value = 7, Name = "Pen Only" },
                new { Value = 8, Name = "Pen/Strep" },
                new { Value = 9, Name = "Pen/Strep/Antibiotic" },
                new { Value = 10, Name = "Strep Only" }
            }
            , "Value", "Name");

        ViewData["additives"] = Additives;

这是我在带有按钮的视图中使用此 SelectList 来添加另一个附加行的地方:

            <div id="freshAdditive">
                <%: Html.Label("--Additives--")%><br />
                <%: Html.DropDownListFor(model => model.freshAdditive[0], (SelectList) ViewData["additives"], string.Empty)%>
                <%: Html.TextBoxFor(model => model.freshAdditiveAmount[0])%>
                <input id="moreAdditive" class="buttoncenter" value="More Additive" type="button" />
            </div>

这是我尝试将 Html.DropDownListFor 与 jQuery 一起使用的地方,这就是我尝试使用 jQuery 添加另一个附加行的地方:(

    <script type="text/javascript">
    // Add additional additive
    $("#moreAdditive").click(function () {
        //alert("More Additive");
        var insertCode = '<br />'
            + '<%: Html.DropDownListFor(model => model.freshAdditive[1],  (SelectList) ViewData["additives"], string.Empty) %>'
            + '<%: Html.TextBoxFor(model => model.freshAdditiveAmount[1])%>'
        $('#freshAdditive').append(insertCode);
    });
</script>

故意使用硬编码元素 1...一旦我掌握了基础知识,我就会重构!)如果我评论在包含 Html.DropDownListFor() 的行中,Html.TextBoxFor() 帮助器似乎与 jQuery 配合得很好。我认为这与多行字符串有关,但还没有找到在 jQuery 中处理它的方法。我确实在这里找到了一些东西,但我无法让它发挥作用。

看起来这应该是直接的,但我没有找到很多建议。有什么想法吗?

谢谢!

I'm running into a problem with using Html.DropDownListFor() with jQuery. I'm using ASP.NET MVC 2, C#, .NET 4 and jQuery 1.4.2.

The error I'm getting is 'Uncaught SyntaxError: Unexpected token ILLEGAL' (seeing this in the console using Chrome's developer tools).

I'll build up to the issue:

Here's the setup of my SelectList in the controller:

            var Additives = new SelectList(
            new[]
            {
                new { Value = 1, Name = "FCS" },
                new { Value = 2, Name = "Fungizone" },
                new { Value = 3, Name = "HEPES" },
                new { Value = 4, Name = "Investigator supplied" },
                new { Value = 5, Name = "L-Glutamine" },
                new { Value = 6, Name = "None" },
                new { Value = 7, Name = "Pen Only" },
                new { Value = 8, Name = "Pen/Strep" },
                new { Value = 9, Name = "Pen/Strep/Antibiotic" },
                new { Value = 10, Name = "Strep Only" }
            }
            , "Value", "Name");

        ViewData["additives"] = Additives;

Here's where I use this SelectList in a view with a button to add another additive row:

            <div id="freshAdditive">
                <%: Html.Label("--Additives--")%><br />
                <%: Html.DropDownListFor(model => model.freshAdditive[0], (SelectList) ViewData["additives"], string.Empty)%>
                <%: Html.TextBoxFor(model => model.freshAdditiveAmount[0])%>
                <input id="moreAdditive" class="buttoncenter" value="More Additive" type="button" />
            </div>

Here's where I'm attempting to use Html.DropDownListFor with jQuery, this is where I'm attempting to add another additive row using jQuery:

    <script type="text/javascript">
    // Add additional additive
    $("#moreAdditive").click(function () {
        //alert("More Additive");
        var insertCode = '<br />'
            + '<%: Html.DropDownListFor(model => model.freshAdditive[1],  (SelectList) ViewData["additives"], string.Empty) %>'
            + '<%: Html.TextBoxFor(model => model.freshAdditiveAmount[1])%>'
        $('#freshAdditive').append(insertCode);
    });
</script>

(Using a hard-coded element of 1 intentionally... I'll refactor once I get the basics working!) If I comment out the line containing Html.DropDownListFor(), the Html.TextBoxFor() helper seems to work just fine with jQuery. I'm thinking this has something to do with multi-line strings, but haven't found a way to handle it in jQuery. I did find somethine here but am unable to get it working.

Seems like this should be straight forward, but I'm not finding many suggestions. Any ideas?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海未深 2024-11-14 05:43:51

另一种方法是将代码渲染到隐藏的 div 中。然后使用 jquery 克隆元素。尝试这样的事情:

<div id="hiddenHtml" style="display:none;">
     <%: Html.DropDownListFor(model => model.freshAdditive[1],  (SelectList) ViewData["additives"], string.Empty) %>
     <%: Html.TextBoxFor(model => model.freshAddit    
</div>
<script type="text/javascript">
// Add additional additive
$("#moreAdditive").click(function () {
    //alert("More Additive");
    var insertCode = $("#hiddenHtml").html();
});

Another way to do this would be to render your code into a hidden div. Then use jquery to clone the elements. Try something like this:

<div id="hiddenHtml" style="display:none;">
     <%: Html.DropDownListFor(model => model.freshAdditive[1],  (SelectList) ViewData["additives"], string.Empty) %>
     <%: Html.TextBoxFor(model => model.freshAddit    
</div>
<script type="text/javascript">
// Add additional additive
$("#moreAdditive").click(function () {
    //alert("More Additive");
    var insertCode = $("#hiddenHtml").html();
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文