简体   繁体   中英

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.

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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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