简体   繁体   English

如何将参数从剃须刀发送到JavaScript函数

[英]How to send parameters from razor to to JavaScript function

I have a table set up to display information from a model. 我设置了一个表格来显示模型中的信息。 Each row has an edit and delete button. 每行都有一个编辑和删除按钮。 The delete button works fine as it passes the models id (area.areaID) to aa javascript function and it works fine. 删除按钮可以正常运行,因为它将模型ID(area.areaID)传递给了javascript函数,并且可以正常运行。 The edit button however is passes all variables of the model to a javascript function but the strings are not passing correctly and only the int variable "area.zone" is being passed correctly. 但是,编辑按钮会将模型的所有变量传递给javascript函数,但字符串传递不正确,并且仅正确传递了int变量“ area.zone”。 The javascript function is suppose to change the placeholders of some inputs in a popup. javascript函数是假定在弹出窗口中更改某些输入的占位符。

The table 桌子

        @foreach (var area in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(ModelItem => area.areaName)
                </td>
                <td>
                    @Html.DisplayFor(ModelItem => area.zone)
                </td>
                <td>
                    @Html.DisplayFor(ModelItem => area.areaCommunity)
                </td>
                <td>
                    @Html.DisplayFor(ModelItem => area.city)
                </td>
                <td>
                    @Html.DisplayFor(ModelItem => area.region)
                </td>
                <td>
                    <input class="btn btn-primary button button4" type="button" value="Edit" onclick="editRecord(@area.areaName, @area.zone, @area.areaCommunity, @area.city, @area.region)"/>
                    <input class="btn btn-danger button button4" type="button" value="Delete" onclick="deleteRecord(@area.areaID)"/>

                </td>


            </tr>
        }

The script function 脚本功能

    function editRecord(editName, editZone, editAC, editCity, editRegion) {
        $('#editModal').modal('show');
        $('#editAreaName').attr("placeholder", editName);
        $('#editZone').attr("placeholder", editZone);
        $('#editAreaCommunity').attr("placeholder", editAC);
        $('#editCity').attr("placeholder", editCity);
        $('#editRegion').attr("placeholder", editRegion);
    }

The form in the popup with the inputs. 带有输入的弹出窗口中的表单。 The zone place holder is being changed if ONLY the zone parameter is passed. 如果仅通过zone参数,则将更改区域占位符。

 <form>
                    <div class="form-group">
                        Area<input class="form-control" type="text"
                               placeholder="" id="editAreaName" />
                    </div>
                    <div class="form-group">
                        Zone<input class="form-control" type="text"
                               placeholder="" id="editZone" />
                    </div>
                    <div class="form-group">
                        Area Community<input class="form-control" type="text"
                               placeholder="" id="editAreaCommunity" />
                    </div>
                    <div class="form-group">
                        City<input class="form-control" type="text"
                               placeholder="" id="editCity" />
                    </div>
                    <div class="form-group">
                        Region<input class="form-control" type="text"
                               placeholder="" id="editRegion" />
                    </div>
                    <div class="form-group">
                        <input type="submit" , value="Update" class="btn btn-primary button button4" id="submitEdit" />
                    </div>
</form>

I am currently getting an error show in the visual studio debugger saying "ReferenceError: BATU is not defined at HTMLInputElement.onclick ( https://localhost:44379/:187:166 )" BATU being an area.areaName. 我目前在Visual Studio调试器中看到一个错误显示,提示“ ReferenceError:未在HTMLInputElement.onclick( https:// localhost:44379 /:187:166 )上定义BATU ”,BATU是area.areaName。 Also if there is a hyphen in the name the value shown in the reference error is cut off before the hyphen 同样,如果名称中有连字符,则参考错误中显示的值会在连字符之前被截断

Put single quotes around each of the parameters you expect to be strings: 用双引号括住您期望是字符串的每个参数:

<input class="btn btn-primary button button4" type="button" value="Edit" onclick="editRecord('@area.areaName', @area.zone, '@area.areaCommunity', '@area.city', '@area.region')"/>

Right now, it is trying to execute editRecord(BATU, /* etc. */) , so it's trying to find an object named BATU instead of passing the string 'BATU', hence the error. 现在,它正在尝试执行editRecord(BATU, /* etc. */) ,因此它试图查找一个名为BATU的对象,而不是传递字符串'BATU',因此出现错误。

Side note: I'm assuming you want to fill in the input boxes with the current values of the item you're editing. 旁注:我假设您要在输入框中填写您正在编辑的项目的当前值。 The placeholder attribute doesn't act as the default value of an input box; 占位符属性不用作输入框的默认值。 it displays the text as a hint of what should go in it, so if the user wanted to use the placeholder as the input value, they would have to type it in manually. 它会显示该文本以提示应该输入的内容,因此,如果用户希望使用占位符作为输入值,则必须手动输入。 Setting the default value can be done by setting its value attribute. 可以通过设置默认值属性value来设置默认值。 Using jQuery: $('#editAreaName').val(editName); 使用jQuery: $('#editAreaName').val(editName);

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

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