我開發了一個asp.net mvc3應用程序。將會有一個初始視圖來搜索客戶列表並加載另一個視圖。我通過ajax $ .get函數來做到這一點。它在我的本地機器上工作正常。但是,這在服務器實例中不起作用。也沒有錯誤消息。Ajax查詢不能在asp.net服務器上工作mvc3
代碼:
SearchCust.cshtml:
@model fbpm.Models.UserDetail
@{
ViewBag.Title = "Customer List";
}
<h2>Search for a Customer</h2>
Customer ID : <input type="text" id="cust" name="cust"/>
<button class="btn btn-primary btn-large" type="button" onclick="handlesearch();"> Search </button>
<br />
<br />
<div id='custlist'> Enter the Customer ID in the above field and click Search </div>
<script type="text/javascript" language="javascript">
$.get('@Url.Action("Index", "User")?id=' + document.getElementById("cust").value,
function (viewResult) {
$("#custlist").html(viewResult);
});
function handlesearch() {
$.get('@Url.Action("Index", "User")?id=' + document.getElementById("cust").value,
function (viewResult) {
$("#custlist").html(viewResult);
});
}
</script>
CODE:index.cshtml:現在
@model IEnumerable<fbpm.Models.UserDetail>
@{
Layout = null;
}
<h2>List of Customers</h2>
<button type="button" name="CreateUser" onclick="createuser();"> Create Customer </button>
<table>
<tr>
<th>
User ID
</th>
<th>
Password
</th>
<th>
Customer Name
</th>
<th>
Email ID
</th>
<th>
Contact #1
</th>
<th>
Contact #2
</th>
<th>
Complete Address
</th>
<th>
State
</th>
<th>
Country
</th>
<th>
Role
</th>
<th>
PAN Number
</th>
<th>
Project Name
</th>
<th>
Booked Date
</th>
<th>
Booked Amount
</th>
<th></th>
</tr>
@foreach (var item in Model) {
if (@Html.DisplayFor(modelItem => item.Role).ToString().Equals("400"))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Password)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Contact1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Contact2)
</td>
<td>
@Html.DisplayFor(modelItem => item.FullAddress)
</td>
<td>
@Html.DisplayFor(modelItem => item.State)
</td>
<td>
@Html.DisplayFor(modelItem => item.Country)
</td>
<td>
@Html.DisplayFor(modelItem => item.Role)
</td>
<td>
@Html.DisplayFor(modelItem => item.PANNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProjectName)
</td>
<td>
@Html.DisplayFor(modelItem => item.BookedDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.BookedAmount)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = @Html.DisplayFor(modelItem => item.UserID) }) |
@Html.ActionLink("Details", "Details", new { id = @Html.DisplayFor(modelItem => item.UserID) }) |
@Html.ActionLink("Delete", "Delete", new { id = @Html.DisplayFor(modelItem => item.UserID) })
</td>
</tr>
}
}
</table>
<script type="text/javascript" language="javascript">
function createuser() {
var url = '@Url.Action("Create")';
window.location.href = url;
}
</script>
,這個動作類:
public ViewResult SearchCust() {
return View();
}
public ViewResult Index(string id)
{
var cust = db.UserDetails.Where(c => c.UserID.Contains(id));
return View(cust.ToList());
}
我的瀏覽器給出了錯誤:它提供了一個錯誤「ReferenceError:jQuery沒有定義」..不知道我應該在這裏做什麼。我在腳本文件中也有jquery 1.9.1 min甚至1.8。
請有人可以幫助我爲什麼這個電話沒有來?
問候, 哈日
也許應該在'$(document).ready(function(){})中編寫你的ajax代碼' – shakib
當你說它不工作時,你需要更具描述性。頁面出現了嗎?瀏覽器調試控制檯中的網絡選項卡顯示什麼? – Jasen
頁面根本沒有出現,並且沒有轉儲。我沒有編寫任何控制檯寫入。 –