繁体   English   中英

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

[英]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.

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