【问题标题】:Ajax query not working from server in asp.net mvc3Ajax 查询在 asp.net mvc3 中的服务器上不起作用
【发布时间】:2013-05-28 14:55:28
【问题描述】:

我开发了一个 asp.net mvc3 应用程序。将有一个初始视图,用于搜索客户列表并加载另一个视图。我通过ajax $.get 函数来做到这一点。它在我的本地机器上运行良好。但是,这在服务器实例中不起作用。也没有错误消息。

代码:

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>

代码: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 is not defined”.. 不知道我应该在这里做什么。我在脚本文件中有 jquery 1.9.1 min 甚至 1.8。

请有人帮我解释为什么这个电话打不通?

问候, 哈里

【问题讨论】:

  • 也许应该在$(document).ready(function(){})中写你的ajax代码
  • 当您说它不起作用时,您需要更具描述性。页面会出现吗?浏览器调试控制台中的网络选项卡显示什么?
  • 页面根本没有出现,也没有转储。而且我还没有为控制台编写任何代码。
  • 如果请求有问题,您的浏览器控制台会告诉您一些信息。
  • 嗨,是的。它给出了一个错误“ReferenceError: jQuery is not defined”。不知道我应该在这里做什么。我在脚本文件中有 jquery 1.9.1 min 甚至 1.8 ..

标签: asp.net ajax asp.net-mvc-3


【解决方案1】:

*终于成功了!! :) 我所要做的就是调用 jquery src 文件并将用户帐户添加到数据库中!

谢谢大家。 *

【讨论】:

    【解决方案2】:

    由于您的评论,请确保 jQuery 库是您加载的第一个脚本,就在您的标记之后。通过您的浏览器确认它实际上也已加载。

    <body>
        <!--your mark-up-->
        <!--jQuery script: either local file or from CDN (jQuery before jQuery UI)-->
        <!--rest of your scripts, putting the scripts that-->
        <!--are depended on in other scripts first-->
    </body>
    

    【讨论】:

    • 你说呢,应该在@model fbpm.Models.UserDetail @{ ViewBag.Title = "Customer List"; }
    • 检查您的浏览器工具并确保库已加载。应该在 Chrome 或 FF 的控制台面板中。
    • 我会试一试。我应该包括这个图书馆吗?
    • 如果您不担心 IE8 及之前的版本,我将仅包含来自 google CDN 的最新 jQuery 和 jQuery UI。如果您需要 IE8 及更早版本,请为 jQuery 使用 1.9.*,因为 jQuery 切断了对 2.0 的支持。我编辑了我的答案:出于显而易见的原因,jQuery 必须出现在 jQuery UI 之前。
    猜你喜欢
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多