[英]How to pass the data from controller to view in mvc4
我正在研究 MVC4 應用程序,因為這是 Actionresult 返回 json 結果,但我想在視圖中傳遞變量 objservice.callid 但我正在返回 json 是否可以在 json 結果的幫助下獲取視圖值或有任何方法將變量的值傳遞給查看,但返回類型應該是 json 結果。
這是控制器中的代碼:
[HttpPost]
public ActionResult create(ServiceCall objservice)
{
AllViewBags();
string result = PartnerMaster.CreateServiceCall(objservice);
if (result == "")
{
ViewBag.id = objservice.callID;
return Json("Service Call = " + objservice.callID + " is Created successfully!");
} else {
return Json("This record is not added because of this error:=>" + result);
}
}
下面是代碼:
if (str.indexOf("successfully") != -1)
{
window.location.href = '@Url.Action("edit", "service_call", new { id = "CC" })'.replace("CC", '@ViewBag.id');
} else {
if (str.search("added") != -1)
{
window.location.href = '@Url.Action("service_call", "service_call")';
} else {
window.location.href = '@Url.Action("edit", "service_call", new { id = "CC" })'.replace("CC", callID);
}
}
我已經嘗試將 objservice.callid 變量存儲在 viewbag 中,並且在視圖上訪問它不起作用。因為視圖不是返回控制器。 是否可以將該變量存儲在會話變量中,然后在視圖中訪問。
請給點建議....
作為具有多個值的 json 對象返回
[HttpPost]
public ActionResult create(ServiceCall objservice)
{
AllViewBags();
string result = PartnerMaster.CreateServiceCall(objservice);
if (result == "")
{
return Json(new { message = "Service Call = " + objservice.callID + " is Created successfully!", id = objservice.callID);
}
else
{
return Json(new {message = "This record is not added because of this error:=>" + result, id = 0});
}
}
並在帖子成功中使用它來重定向...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.