简体   繁体   English

Ajax查询无法从ASP.NET MVC3中的服务器工作

[英]Ajax query not working from server in asp.net mvc3

I have developed an asp.net mvc3 application. 我已经开发了一个asp.net mvc3应用程序。 There will be an initial view which searches for a list of customer and loads another view. 将有一个初始视图,该视图搜索客户列表并加载另一个视图。 I am doing this through ajax $.get function. 我通过ajax $ .get函数来执行此操作。 It was working fine in my local machine. 在我的本地机器上运行正常。 However, the same is not working in the server instance. 但是,这在服务器实例中不起作用。 There are no error messages also. 也没有错误消息。

Code: 码:

SearchCust.cshtml: 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: 代码: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>

Now, the action class for this is: 现在,此操作的类为:

public ViewResult SearchCust() {
    return View();
}

public ViewResult Index(string id)
{
   var cust = db.UserDetails.Where(c => c.UserID.Contains(id)); 
   return View(cust.ToList());
}

My browser gives the error: It gives an error "ReferenceError: jQuery is not defined".. Not sure what I should be doing here. 我的浏览器给出了错误:它给出了错误“ ReferenceError:jQuery未定义”。。不确定我在这里应该做什么。 I have the jquery 1.9.1 min and even 1.8 also in the script files. 我在脚本文件中有jquery 1.9.1 min甚至1.8。

Please can someone help me on why this call is not coming up? 请有人能帮我解决为什么没有打这个电话吗?

Regards, Hari 问候,哈里

Due to your comment, make sure the jQuery library is the first script you load, right after your mark-up. 由于您的评论,请确保在标记之后立即将jQuery库作为第一个加载的脚本。 Confirm through your browser that it is actually loaded as well. 通过浏览器确认它实际上也已加载。

<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>

*It finally works!! *终于可以了!! :) All I had to do is just mcall the jquery src files and also add the user account to the Database! :)我要做的就是只是调用jquery src文件,并将用户帐户添加到数据库中!

Thank you all. 谢谢你们。 * *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM