按鈕關聯腳本我有一個按鈕和一個腳本:在ASP .NET MVC Razor視圖
<script>
function SubmitClick() {
var pid = $(this).data('personid');
var sid = $(this).data('surveyid');
var url = '@Url.Action("SubmitSurvey", "Person")';
$.post(url, { personid: pid, surveyid: sid }, function (data) {
alert('updated');
});
};
</script>
當我點擊按鈕的腳本將不運行/被叫/調用。如何使這個按鈕調用這個腳本?
完全視圖_Survey1.html
這是PartialView:
<script>
function SubmitClick() {
var pid = $(this).data('personid');
var sid = $(this).data('surveyid');
var url = '@Url.Action("SubmitSurvey", "Person")';
$.post(url, { personid: pid, surveyid: sid }, function (data) {
alert('updated');
});
};
</script>
@using WebApplication2.Models
@model System.Tuple<Person, List<Survey>>
<hr />
<h1>Surveys</h1>
<input type="button" id="Coll" value="Collapse" onclick="javascript:CollapseDiv()" />
@*<p>
Number of Surveys: @Html.DisplayFor(x => Model.Item2.Count)
</p>*@
@{int i = 1;}
@foreach (var survey in Model.Item2) {
using (Html.BeginForm()) {
<h2>Survey @(i)</h2>
<p />
@Html.EditorFor(x => survey.Questions)
<button class='mybutton' type='button' data-personid="@Model.Item1.Id" data-surveyid="@survey.Id" onclick="javascript:SubmitClick()">Click Me</button>
}
i++;
<hr style="background-color:rgb(126, 126, 126);height: 5px" />
}
<hr />
的SubmitSurvey
方法PersonController
:
public void SubmitSurvey(int personId, int surveyId) {
System.Diagnostics.Debug.WriteLine("UPDATING DATABASE");
}
結果是,點擊按鈕後,我得到錯誤說SubmitClick
避風港未找到:
Details.cshtml(_Survey1.cshtml)被渲染到裏面。
@model WebApplication2.Models.Person
@{
ViewBag.Title = "Details";
}
<script>
function BtnOnclick() {
$.ajax({
type: 'POST',
url: '@Url.Content("~/Person/_Survey1")',
data: {
id: '@Model.Id'
},
success: function (data) {
$('#divpopup').css("display", "block");
$('#btnExpand').css("display", "none");
$('#divpopup')[0].innerHTML = data;
}
});
}
function CollapseDiv() {
$('#divpopup').css("display", "none");
$('#btnExpand').css("display", "block");
}
</script>
<h2>Details</h2>
<div>
<h4>Person</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.FirstName)
</dt>
<dd>
@Html.DisplayFor(model => model.FirstName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.LastName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CellNumber)
</dt>
<dd>
@Html.DisplayFor(model => model.CellNumber)
</dd>
<dt>
@Html.DisplayNameFor(model => model.SecondaryPhoneNumber)
</dt>
<dd>
@Html.DisplayFor(model => model.SecondaryPhoneNumber)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Address)
</dt>
<dd>
@Html.DisplayFor(model => model.Address)
</dd>
<dt>
@Html.DisplayNameFor(model => model.BirthDate)
</dt>
<dd>
@Html.DisplayFor(model => model.BirthDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Pesel)
</dt>
<dd>
@Html.DisplayFor(model => model.Pesel)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Notes)
</dt>
<dd>
@Html.DisplayFor(model => model.Notes)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Status)
</dt>
<dd>
@Html.DisplayFor(model => model.Status.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.PersonalDataProcessing)
</dt>
<dd>
@Html.DisplayFor(model => model.PersonalDataProcessing)
</dd>
</dl>
</div>
<!--BEGIN-->
<p>
<input type="button" value="Expand" id="btnExpand"
onclick="javascript:BtnOnclick();" />
</p>
<div id="divpopup" style="display:none">
</div>
<!--END-->
<p>@Html.ActionLink("Call Cell Phone", "Call", new { id = Model.Id, number = Model.CellNumber }, new { @class = "btn btn-default" })</p>
<p> @Html.ActionLink("Call Client's Secondary Number »", "Call", new { id = Model.Id, number = Model.SecondaryPhoneNumber }, new { @class = "btn btn-default" })</p>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Id })
@Html.ActionLink("Back to List", "Index")
</p>
有一個答案,發生了什麼? – Yoda 2014-08-28 23:47:39
始終包含按鈕的'type'屬性。不同的瀏覽器使用不同的默認值,因此可能默認提交。'' – 2014-08-28 23:53:55
@StephenMuecke我完全更新了原始帖子 - >代碼和說明。我向HTML添加了'type ='button''。它防止了死亡的黃色屏幕,沒有運行時錯誤,但點擊後沒有任何反應,控制檯上沒有任何輸出或任何東西。 – Yoda 2014-08-29 00:06:34