简体   繁体   English

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

[英]How to pass the data from controller to view in mvc4

I am working on MVC4 application in that this is Actionresult returns json result but i want to pass variable objservice.callid on view but i am returning json can it is possible to get value on view with the help of json result or having any method to pass the value of variable to view but return type shoould be json result.我正在研究 MVC4 应用程序,因为这是 Actionresult 返回 json 结果,但我想在视图中传递变量 objservice.callid 但我正在返回 json 是否可以在 json 结果的帮助下获取视图值或有任何方法将变量的值传递给查看,但返回类型应该是 json 结果。

Here is code in controller:这是控制器中的代码:

[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);
    }            
}

Here is code in view:下面是代码:

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);
    }
}

I have try that objservice.callid variable store in viewbag and access on view it is not work.because view is not return controller.我已经尝试将 objservice.callid 变量存储在 viewbag 中,并且在视图上访问它不起作用。因为视图不是返回控制器。 can it is possible to store that variable in session variable then access on view.是否可以将该变量存储在会话变量中,然后在视图中访问。

Please give some suggestion ....请给点建议....

return as a json object with multiple values作为具有多个值的 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});
        }            
    }

and use this in the post success to redirect ...并在帖子成功中使用它来重定向...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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