简体   繁体   English

如何将C#与javascript混合以更改html?

[英]How to mix C# with javascript for changing html?

I'm trying to change a html DOM element. 我正在尝试更改html DOM元素。 With data from asp.net. 来自asp.net的数据。 All the data from CSharp is in the ViewBags. 来自CSharp的所有数据都在ViewBags中。 ViewBag.Kenmerken is a list of Strings and ViewBag.Definities is a list a definition object with a variable 'variabel' and the variable 'kenmerk'. ViewBag.Kenmerken是字符串的列表,而ViewBag.Definities是具有变量'variabel'和变量'kenmerk'的定义对象的列表。 I have the following code: 我有以下代码:

@{
    ViewBag.Title = "KenmerkSelectie";
}


<script type="text/javascript">
    function Kenmerk1() {
        var myselect = document.getElementById("kenmerk1");
        var selectValue = myselect.options[myselect.selectedIndex].value;
        var newOptions= "";

        @foreach (var def in ViewBag.Definities) {

            //@def.kenmerk is C# && selectValue is JS;
            if(@def.Kenmerk == selectValue){
                newOpstions= newOptions+ "<option value=\"@def.Variabel\"> @def.Variabel </option>";
            }
        }

        document.getElementById("var1").innerHTML = newOptions;
    }

    <div class="kenmerk">
        <h3>Kenmerk 1</h3>
                <select id="kenmerk1" onchange="Kenmerk1()">

                    @foreach (var item in ViewBag.Kenmerken)
                {
                    <option value="@item">
                            @item
                        </option>
                }


                </select>


                <select id="var1" multiple="multiple">
                    <!-- add option from js function -->
                </select>
            </div>

EDIT: 编辑:

Description: An error occurred during the compilation of a resource required to service this request. 说明:编译服务于此请求所需的资源期间发生错误。 Please review the following specific error details and modify your source code appropriately. 请查看以下特定的错误详细信息,并适当地修改您的源代码。

Compiler Error Message: CS0103: The name 'selectValue' does not exist in the current context 编译器错误消息:CS0103:当前上下文中不存在名称“ selectValue”

Source Error: if(@def.Kenmerk == selectValue){ 源错误:if(@ def.Kenmerk == selectValue){

You aren't properly escaping your " (quotations). This line: 您没有正确转义" (引号)。此行:

newOptions = newOptions + "<option value="@def.Variabel"> @def.Variabel </option>";

Should be something like: 应该是这样的:

newOptions = newOptions + @"<option value=""@def.Variabel""> @def.Variabel</option>";

Though, for ASP.NET MVC design principles, I'd recommend not using the ViewBag. 但是,对于ASP.NET MVC设计原则,我建议不要使用ViewBag。 Use a model for passing in your data instead. 请改用模型传递数据。 http://tech.trailmax.info/2013/12/asp-net-mvc-viewbag-is-bad/ http://tech.trailmax.info/2013/12/asp-net-mvc-viewbag-is-bad/

EDIT: Also, it looks like you should convert your data to JSON so that you can compare your values on the JavaScript side. 编辑:而且,看起来您应该将数据转换为JSON,以便可以在JavaScript端比较值。 Since def.Kenmerk lives in C#, it won't be able to evaluate the JavaScript variable, selectValue on an == comparison. 由于def.Kenmerk在C#中,因此无法在==比较中评估JavaScript变量selectValue

Try using razors <text> Pseudo-element see also here 尝试使用剃须刀<text>伪元素另请参见此处

function Kenmerk1() {
    var myselect = document.getElementById("kenmerk1");
    var selectValue = myselect.options[myselect.selectedIndex].value;
    var newOptionsInHtmlStyle = "";

    @foreach (var def in ViewBag.Definities) {
        <text>
        //@def.kenmerk is C# && selectValue is JS;
        if(@(def.Kenmerk) == selectValue){
            newOptions = newOptions + '<option value="@(def.Variabel)"> @(def.Variabel) </option>';
        }
        </text>
    }

    document.getElementById("var1").innerHTML = newOptions;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM