在我的网页上,我有一个表,该表显示5行数据,当单击这些行之一时,将获取请求发送到控制器类中的方法,该方法返回一些数据并使用此数据填充表单。
我的主要问题是,根据我重定向到该页面的方式(从登录页面或其他页面),此获取请求并不总是有效。
如果我从登录名重定向到此页面,则工作正常,但是从其他地方,不会调用get请求。我曾尝试在控制器方法中添加断点,但该程序从未命中它(除非从登录重定向)。
我尝试将Jquery.click方法添加到$(document).ready(function(){code ..})中的函数中,但这没什么区别。
控制器方法
//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>
答案 0 :(得分:1)
添加/每个网址。如果您的项目在文件夹“ @ Url.Content(”〜/“)”下运行,请使用。
...
$.get('/TimesheetEntry/GetEntryFromRecentEntries',{value: 'value'})
...
或
...
$.get('@(Url.Content("~/"))TimesheetEntry/GetEntryFromRecentEntries',{value: 'value'})
...