我有一個網站,我顯示學生信息。我想在網站上添加幾個文本字段,並使用按鈕單擊上的jQuery(Ajax)異步更新這些字段。我相信我有所有的要求,但數據仍未更新。按鈕點擊不觸發jQuery(Ajax)功能更新頁面
我在這裏錯過了什麼嗎?點擊按鈕什麼也不做。
這裏是我的代碼 -
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Student.Models;
namespace Student.Controllers
{
public class StudentController : Controller
{
public ActionResult Index()
{
return View("Student");
}
[HttpPost()]
public ActionResult DisplayStudentName(string id)
{
StudentDataContext db = new StudentDataContext();
var StudentName = (from p in db.vwStudent.Where(a => a.StudentId == id)
group p by p.StudentName into g
select g.Key).FirstOrDefault();
ViewData["StudentName"] = StudentName;
return View("Student");
}
[HttpPost()]
public ActionResult DisplayStudentStatus(int? id, string flg)
{
AccountDataContext db = new AccountDataContext();
var StudentStatus = (from p in db.vwStudent.Where(a => a.StudentId == id && a.LastFlag == flg)
group p by p.Status into g
select g.Key).FirstOrDefault();
ViewData["StudentStatus "] = StudentStatus;
return View("Student");
}
}
}
的jQuery:
$("#Button1").click(function() {
var link = '<%= Url.Action("DisplayStudentName", "Student")';
$.ajax({
url: link,
data: "{id: '<%= ViewContext.RouteData.Values["id"] %>'}",
dataType: "html",
success: Success,
error: Fail
});
});
$("#Button2").click(function() {
var link = '<%= Url.Action("DisplayStudentStatus", "Student")';
$.ajax({
url: link,
data: "{id: '<%= ViewContext.RouteData.Values["id"] %>' ,
flg: '<%= ViewContext.RouteData.Values["flg"] %>'}",
dataType: "html",
success: Success,
error: Fail
});
});
function Success(){
alert("Success");
}
function Fail(){
alert("Fail");
}
查看:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Student Form
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" method="get" runat="server">
Student ID:<input type="text" name="id" id="StudentId" value="<%=HttpContext.Current.Request.QueryString["id"]%>" /><br />
Student Name:<input type="text" name="StudentName" id="StudentName" value="<%=ViewData["StudentName"]%>"/>
<div id="Btn1"><input type="button" value="Display Student Name" name="Btn1" id="Button1" />
</div>
Student Status:<input type="text" name="StudentStatus" id="StudentStatus" value="<%=HttpContext.Current.Request.QueryString["StudentStatus"]%>" />
<div id="Btn2"><input type="button" value="Display Profit Center" name="Btn2" id="Button2" />
</div>
</div>
</form>
</asp:Content>
在此先感謝
你想事情發生時,該按鈕的onClick引發火災,對吧?我認爲你需要一個onClick屬性。 –
你有沒有檢查過你沒有收到Javascript錯誤的地方。例如,使用Chrome瀏覽器可以查看控制檯日誌? @BobHorn'$(「#Button1」)。click'包含單擊按鈕時運行腳本的處理程序。 – Michael