在我的视图页面上,我通过ajax请求,在控制器操作上将一些值传递给控制器,在检查后,将消息值重定向到视图的控制器。将消息添加到模型和pasisng模型以再次使用新模型值查看。第二个时间(回发)模型值作为Json传递给视图,但新模型值(即消息)不能被javascript捕获。在我的代码中它是Model.INFO
$.ajax({
type: "POST",
url: '@Url.Action("TeamSaveChanges", "Administrator")',
data: {
ID: '@Model.ID',
doctorID: doctorValue,
nurseID:nurseValue,
driverID:driverValue,
technicianID: technicianValue
},
dataType: "text",
success: function () { alert("@Model.INFO")},
error: function () { alert("Error occured!!!") }
});
控制器
public ActionResult TeamSaveChanges(Guid ID, Guid? doctorID, Guid? nurseID, Guid? driverID, Guid? technicianID)
{
try
{
using (var client = SoapProxyFactory.CreateDSrvGDSoapClient())
{
var emptyTeam = Guid.Empty;
var ambID = client.getAmbulanceIDbyTeamID(ID);
var baseresult = client.checkAmblanceTeamsforDuplicateMembers(ambID, ID);
if (doctorID == emptyTeam && nurseID == emptyTeam && driverID == emptyTeam && technicianID == emptyTeam )
{
var result = client.EditTeamMembers(ID, doctorID, nurseID, driverID, technicianID);
if (result)
throw new Exception("saved");
}
else
{
foreach (var item in baseresult)
{
if(item.DOCTORCODE == doctorID && item.NURSECODE == nurseID && item.DRIVERCODE == driverID && item.TECHNICIANCODE == technicianID)
throw new Exception("The team with Same Members is exist." + "<p>(" + item.TEAMCODE + ")</p>");
}
var result = client.EditTeamMembers(ID, doctorID, nurseID, driverID, technicianID);
if (result)
throw new Exception("saved");
}
catch (Exception exp)
{
string message = exp.Message;
return RedirectToAction("TeamMembers", "Administrator", new { ID = ID, message = message });
}
[OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
public ActionResult TeamMembers(Guid? ID,string message)
{
try
{
if (!ID.HasValue())
return RedirectToAction("Ambulance");
using (var client = SoapProxyFactory.CreateDSrvALLSoapClient())
{
Guid id = ID.Value;
var clientGD = SoapProxyFactory.CreateDSrvGDSoapClient();
var result = client.GetTeamMembers(id);
result.INFO = message;
if (message != null)
{
result.INFO = message;
return Json(result,JsonRequestBehavior.AllowGet);
}
return View(result);
}
}
答案 0 :(得分:0)
这一行:
success: function () { alert("@Model.INFO")},
只会将模型的INFO拉入一次,因为它会在客户端中呈现服务器值。如果您希望它发生变化,那么您必须将结果传递回成功,并接受新值:
success: function (d) { alert(d); },
要向其返回值,您必须从操作返回:
return Content("SOMEVAL"); // or PartialView or something that is string data
但是,重定向到操作不会返回对调用者的响应,并且可能无法通过AJAX正确处理,所以我不能100%确定问题是什么......
答案 1 :(得分:0)
为什么要使用AJAX呢?发生了什么事情是你的脚本向你的控制器发出一个请求,它将响应作为数据发回,而不是重定向到新的网页。
只需创建一个表单,以典型的MVC方式将这些变量POST到您的控制器,您将获得所需的结果。