在我的网页上,我有一个显示 5 行数据的表格,当单击其中一行时,将向 Controller 类中的一个方法发送一个 get 请求,该方法返回一些数据并使用这些数据填充表单。
我的主要问题是,根据我重定向到此页面的方式(从我的登录页面或从其他页面),此获取请求并不总是有效。
如果我从登录重定向到此页面,则效果很好,但从其他地方,不会调用 get 请求。我尝试在 Controller 方法中添加断点,但程序从未命中它(除非从登录重定向)。
我尝试将 Jquery.click 方法添加到 $( document ).ready(function() { code here.. }) 中的函数中,但这没有什么区别。
Controller 方法
//This function returns a CSV of data
public string GetEntryFromRecentEntries(string userID, int index)
{
string entryID = this.GetRecentEntries(userID, 5)[index][0];
DOCOsoft.DataAccess.DatabaseReader dBReader = new DOCOsoft.DataAccess.DatabaseReader();
List<SqlParameter> parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("@entryID", entryID));
StringBuilder entryData = new StringBuilder();
using (IDataReader singleEntryReader = dBReader.ExecuteReader(ConfigurationManager.ConnectionStrings[1].ToString(), "get_entries", parameters))
{
while (singleEntryReader.Read())
{
entryData.Append(singleEntryReader["CompanyID"].ToString() + ",");
entryData.Append(singleEntryReader["ProjectID"].ToString() + ",");
entryData.Append(singleEntryReader["JobID"].ToString() + ",");
entryData.Append(singleEntryReader["DurationInHours"].ToString() + ",");
entryData.Append(singleEntryReader["Notes"].ToString());
}
}
return entryData.ToString();
}
查看
<div class="modal" id="addJobModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header2 right">
<a class="col-md-offset-11 btn btn-default" data-dismiss="modal" aria-hidden="true"><i class="glyphicon glyphicon-remove "></i></a>
</div>
<div class="modal-body">
@using (Html.BeginForm("AddTimesheetEntry", "Home", new { date = ViewBag.Date }, FormMethod.Post, new { @id = "CreateTimesheetEntryForm", @data_ProjectAreaListAction = @Url.Action("GetProjectAreas") }))
{
@Html.Hidden("UserID", User.Identity.GetUserId())
<label class="col-lg-3">Company</label>
@Html.ValidationMessage("CompanyID", "Company is required")
@Html.DropDownList("CompanyID", (SelectList)ViewBag.Companies, "", new { @class = "form-control", @id = "CompanyID", @required = "required" })
<br />
<!-- TODO - If statement here to check if Company ID matches Compnay ID in Projects and if visible?-->
<label class="col-lg-3">Project</label>
@Html.ValidationMessage("ProjectID", "Project is required")
@Html.DropDownList("ProjectID", (SelectList)ViewBag.Projects, "", new { @class = "form-control", @id = "ProjectID", @disabled = "disabled", @required = "required" })
<br />
<label class="col-lg-3">Job</label>
@Html.ValidationMessageFor(m => m.JobID, "Job is required")
@Html.DropDownListFor(m => m.JobID, (SelectList)ViewBag.Jobs, "", new { @class = "form-control", @id = "JobID", @required = "required" })
<br />
<!--
This form continues down but I have cut off the end to shorten this code block
.
.
.
-->
<div class="recent-entry-padding">
<div class="recent-entry-popup">
<div class="recent-entry-title">Recent Entries</div>
<table id="recent-entry-table">
<tr class="recent-entry-header">
<th>Date</th>
<th>Company</th>
<th>Project</th>
<th>Job</th>
<th>Notes</th>
<th>Hours</th>
</tr>
@{bool alternatingRow = true; }
@foreach (string[] recentEntry in ViewBag.RecentTimesheetEntries)
{
string className = alternatingRow == true ? "recent-entry-row" : "recent-entry-row alternating-row";
<tr class="@className">
<td>@recentEntry[1]</td>
<td>@recentEntry[2]</td>
<td>@recentEntry[3]</td>
<td>@recentEntry[4]</td>
<td>@recentEntry[5].Substring(0, recentEntry[5].Length > 30 ? 30 : recentEntry[5].Length) </td>
<td class="recent-entry-row number">@recentEntry[6]</td>
</tr>
alternatingRow = !alternatingRow;
}
</table>
</div>
</div>
<script>
.
.
.
$(".recent-entry-row").click(function () {
alert('@ViewBag.CurrentUserID');
var entryData = $.get('TimesheetEntry/GetEntryFromRecentEntries', { userID: '@ViewBag.CurrentUserID', index: this.sectionRowIndex - 1 }, function (data) {
alert("Jquery.get has called the the controller method, this does not popup if not redirecting through login page");
//Do Something
});
});
.
.
.
</script>
最佳答案
为每个网址添加/。如果您的项目在文件夹“@Url.Content("~/")”下运行,请使用。
...
$.get('/TimesheetEntry/GetEntryFromRecentEntries',{value: 'value'})
...
或
...
$.get('@(Url.Content("~/"))TimesheetEntry/GetEntryFromRecentEntries',{value: 'value'})
...
关于javascript - Jquery.Get 调用 Controller 方法并不总是有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55866556/