Search code examples
asp.netasp.net-mvc-3viewbag

How to hide ViewBag in asp.net MVC3 razor View


I am rendering a new view from some view(say mainView) by using Html.ActionLink("","",new{id=packageID})
and my controller action(ActionLink passing control to) look like
public ActionResult editChecks(int packageID) {return View();}
Now at Rendered View("editChecks") i want to hide packageID so that i can use it(by passing it to another controller action) on another action by any means

i.e I am using form inside View("editChecks") which is submitting data to some action say(action2)


Presently I am using this in my view("editChecks")
and string pid = Request.Form["packageID"]; at my controller action(need packageID) is this works fine for long run??? Or is there any alternate i should go for???


Solution

  • You can declare any expected parameters in the method. You can also declare a default if it is not supplied String packageID = "0"

    EDIT: your question is confusing. If you want to access it on the view, use viewbag.

    public ActionResult action2(Int32 packageID) {
    
      // can use packageID here
    
     // Or make it accessable on the view
     ViewBag.PackageID = packageID;
      return View();
    }
    

    On the view you can access this by typing

    @ViewBag.PackageID
    

    Though I question why your packageID is a String in some cases. Should you not be using Int32?

    EDIT: Going on your comments,

    @Html.Hidden("PackageID", (Int32)@ViewBag.PackageID)
    

    Will have the attribute on the page, hidden. And will be passed to any called submit (assuming within the form) as the name "PackageID"