如何将数据从控制器传递到mvc4

时间:2016-11-03 11:00:23

标签: javascript json asp.net-mvc

我正在研究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);
    }
}

我在viewbag中尝试了objservice.callid变量存储,并且在视图上访问它不起作用。因为视图不是返回控制器。 是否可以将该变量存储在会话变量中,然后在视图上进行访问。

请提出一些建议......

1 个答案:

答案 0 :(得分:0)

以具有多个值的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});
        }            
    }

并在成功后重新定位...