M-Fulu
M-Fulu

Reputation: 91

Viewbag from controller (JsonResult) to razor view as hidden field

I have the controller function that returns a Json and in side the function I am also populating a viewbag, this viewbag value I want to pass it through to the view as a hidden field which I am not too sure how.

Controller Function:

   public JsonResult Person(string name, string Lastname)
        {
            User user = System.Web.HttpContext.Current.Session["User"] as User;
            bool moreResults = false;
            string response = string.Empty;
            Index model = new Index();

            Collection<ListItem> results = new Collection<ListItem>();

            try
            {
                if (user == null)
                    return Json(new { faultType = NotificationType.Denied.ToString(), responseText = string.Empty, responseAction = "window.top.location.reload(true);", resultsList = string.Empty }, JsonRequestBehavior.AllowGet);

                Tuple<LogisFault, Collection<ListItem>> lookupResponse = Lookups.GetPersonList(name, Lastname);
                results = lookupResponse.Item2;

                if (lookupResponse.Item1.Message.StartsWith("MORE"))
                {
                    moreResults = true;
                    response = lookupResponse.Item1.Message.Split('-')[1].Trim();
                    return Json(new { faultType = NotificationType.Success.ToString(), responseText = response, hasMoreResults = moreResults, responseAction = string.Empty, resultsList = results }, JsonRequestBehavior.AllowGet);
                }

                if (lookupResponse.Item1.FaultType == NotificationType.Warning)
                {
                    string action = Notifications.CreatePopupNotification(lookupResponse.Item1.Message, NotificationType.Warning);
                    return Json(new { faultType = NotificationType.Warning.ToString(), responseText = lookupResponse.Item1.Message, hasMoreResults = moreResults, responseAction = action, resultsList = results }, JsonRequestBehavior.AllowGet);
                }
                ViewBag.CanUserEdit = Security.ValidationMethods.CanUserEdit();
                ViewBag.AllowLink = Security.ValidationMethods.AllowLink();

                return Json(new { faultType = NotificationType.Success.ToString(), responseText = string.Empty, hasMoreResults = moreResults, responseAction = string.Empty, resultsList = results }, JsonRequestBehavior.AllowGet);

            }}

View hidden field returns as empty string

<input type="hidden" name="canUserEdit" id="canUserEdit" value="@ViewBag.CanUserEdit" />

What I want to do is to save that viewbag value to a hidden field on the view and use the hidden field on my external JavaScript file

Upvotes: 0

Views: 47

Answers (1)

M-Fulu
M-Fulu

Reputation: 91

Thank you I have just added CanUserEdit and AllowLink on my model and from there I saved it as a hidden field on my razor view so that I can use it on my external JavaScript file. thank you everyone.

Upvotes: 0

Related Questions