我在masterpage中添加了一个js文件。并在View中使用它。
<div class="control-group">
@Html.LabelFor(model => model.Computers, new { @class = "control-label" })
<div id="computersEditorRows" style="clear: both; margin-right: 30px; padding: 10px; border: 1px solid rgb(204, 204, 204);">
@foreach (var item in Model.Computers)
{
Html.RenderPartial("_ComputersEditorRow", item);
}
</div>
<a id="addItemcomputer" style="cursor: pointer;">AddItem</a>
</div>
和js文件
$(document).ready(function () {
$("#addItemcomputer").click(function () {
$.get("/TechnicalOfficerService/AddComputerNewRow", function (data) {
$("#computersEditorRows").append(data);
}).fail(function (xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
});
});
})
但是当我在Partialview中使用它时,不要调用函数。我在局部视图中使用此HTML代码,但不要调用$("#addItemcomputer").click
。
部分视图
@model PSYCO.Web.Sepid.ViewModels.ComputerViewModel
@using PSYCO.Web.Sepid.Helpers;
<div class="control-group">
@using (Html.BeginCollectionItem("Computers"))
{
<div class="control-group">
@Html.LabelFor(model => model.Computers, new { @class = "control-label" })
<div id="computersEditorRows" style="clear: both; margin-right: 30px; padding: 10px; border: 1px solid rgb(204, 204, 204);">
@foreach (var item in Model.Computers)
{
Html.RenderPartial("_ComputersEditorRow", item);
}
</div>
<a id="addItemcomputer" style="cursor: pointer;">Add Item</a>
</div>
<a class="removeItemNationality" style="cursor: pointer;">Delete</a>
}
</div>
修改
在局部视图中添加此部分。
@section Scripts
{
<script>
$(document).on("click","#addItemcomputer",function () {
alert('d');
})
</script>
}
答案 0 :(得分:0)
应在Partial View中添加脚本。在Partial View中使用脚本链接。
<script type="text/javascript" src="@Url.Content("/Scripts/SomeScript.js")"></script>
或者在部分视图中使用脚本标记。(不能在部分视图中使用部分)
<script>
$("#addItemcomputer").click(function () {
$.get("/TechnicalOfficerService/AddComputerNewRow", function (data) {
$("#computersEditorRows").append(data);
}).fail(function (xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
});
});
</script>