I'm a newbie to MVC, so apologies if this might be poorly ignorant. In my MVC application,I'm trying to track every single time a page is clicked or accessed. My strategy is to log it on the database whenever a click happens on the home view for a particular page, by passing a parameter to a javascript function, which in turn calls a webmethod that communicates with the cs file.
我是MVC的新手,如果这可能是无知的,那么道歉。在我的MVC应用程序中,我试图跟踪每次单击或访问页面的时间。我的策略是通过将参数传递给javascript函数,只要在特定页面的主视图上发生单击就将其记录在数据库上,javascript函数又调用与cs文件通信的web方法。
My main problem at the moment is that I am unable to pass data from the click on the home page to the javascript function, for some very weird reason, it is refusing to communicate with the function. I tried using two methods, onClick and data-bind (both strategies represented in the code below) and it doesnt work. Any advice?
我目前的主要问题是我无法将数据从主页上的点击传递到javascript函数,出于某些非常奇怪的原因,它拒绝与该函数通信。我尝试使用两种方法,onClick和数据绑定(这两种策略在下面的代码中表示)并且它不起作用。任何建议?
<tr class="row">
<td>
<div class="tabbable">
<ul class="nav nav-tabs nav-stacked" id="menuTabs">
<li class="active"><a href="#land" data-toggle="tab" onclick="PostName('Home')">Home</a></li>
<li class=""><a href="#tutuwaroadshow" data-toggle="tab" data-bind="click: function (o, e){$root.PostName(o,e);}" >Tutuwa roadshows</a></li>
<li class=""><a href="#faqsandpresentations" data-toggle="tab" onclick="PostName('Faqs')">FAQ's and presentations</a></li>
<li class=""><a href="#examplescenarios" data-toggle="tab" onclick="PostName('ExampleScenarios')">Example scenarios</a></li>
<li class=""><a href="#financialadvisors" data-toggle="tab" onclick="PostName('FinancialAdvisors')">Financial advisors</a></li>
<!--<li class=""><a href="#privateclients" data-toggle="tab">Private Clients</a></li>-->
<li class=""><a href="#trustdeeds" data-toggle="tab" onclick="PostName('TrustDeeds')">Trust deeds</a></li>
<li class=""><a href="#newsflashes" data-toggle="tab" onclick="PostName('Newsflashes')">Newsflashes</a></li>
<li class=""><a href="#usefullinksandcontacts" data-toggle="tab" onclick="PostName('Useful')">Useful links and contacts</a></li>
<li class=""><a href="#webinar" data-toggle="tab" onclick="PostName('Webinar')">Webinar</a></li>
</ul>
</div>
<div class="newsflash"><a href="~/Content/files/Tutuwa%20Newsflash%204.pdf" target="_blank">Click here for the latest newsflash.</a></div>
</td>
<td rowspan="2">
<div class="tab-content">
<div class="tab-pane active" id="land">@Html.Partial("Land")</div>
<div class="tab-pane" id="tutuwaroadshow">@Html.Partial("TutuwaRoadshow")</div>
<div class="tab-pane" id="faqsandpresentations">@Html.Partial("FaqsAndPresentations")</div>
<div class="tab-pane" id="examplescenarios">@Html.Partial("ExampleScenarios")</div>
<div class="tab-pane" id="financialadvisors">@Html.Partial("FinancialAdvisors") </div>
<div class="tab-pane" id="privateclients">@Html.Partial("PrivateClients")</div>
<div class="tab-pane" id="trustdeeds">@Html.Partial("TrustDeeds")</div>
<div class="tab-pane" id="newsflashes">@Html.Partial("NewsFlashes")</div>
<div class="tab-pane" id="usefullinksandcontacts">@Html.Partial("UsefulLinksAndContacts")</div>
<div class="tab-pane" id="webinar">@Html.Partial("Webinar")</div>
</div>
</td>
And the javascript:
和javascript:
<script type="text/javascript">
function PostName(pageName) {
debugger;
$.ajax({
type: "POST",
url: 'HomeController/SaveVisitorHits',
data: { s: pageName },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
//do nothing
},
error: function (e) {
// do nothing
}
});
}
</script>
1 个解决方案
#1
1
You need to define the function before you use it in your html, and you need to write onclick="javascript:PostName('name')"
.
您需要在html中使用它之前定义该函数,并且需要编写onclick =“javascript:PostName('name')”。
A better way, however, would be to set the name in a data-attribute and attach the click handler using jQuery, since you're already using that for the AJAX call.
但是,更好的方法是在数据属性中设置名称并使用jQuery附加单击处理程序,因为您已经将其用于AJAX调用。
#1
1
You need to define the function before you use it in your html, and you need to write onclick="javascript:PostName('name')"
.
您需要在html中使用它之前定义该函数,并且需要编写onclick =“javascript:PostName('name')”。
A better way, however, would be to set the name in a data-attribute and attach the click handler using jQuery, since you're already using that for the AJAX call.
但是,更好的方法是在数据属性中设置名称并使用jQuery附加单击处理程序,因为您已经将其用于AJAX调用。