Reputation: 2022
in my controller i'm passing an id , Storing it in a ViewBag and then accessing it from the view and its giving a null entry
This is the controller :
public ActionResult Create(int referenceID)
{
ViewBag.id = referenceID;
// ViewBag.type = type;
// ViewBag.title = title;
ViewBag.userID = new SelectList(db.UserProfiles, "userID", "firstName");
return View();
}
and this is how i'm accessing it from my view
@{ Model.referenceID = ViewBag.id ; }
When i'm going to /Invitation/Create/1 its giving me a null entry error.
Upvotes: 1
Views: 177
Reputation: 1857
First you need to make sure that a routing path is configured for /Invitation/Create/{id} in your route table...in global.asax...i.e. in your the RegisterRoutes method of global.asax...make sure you have something like...
// Invitation/Create
routes.MapRoute(
"Create Invitation",
"Invitation/Create/{id}",
new { controller = "Invitation", action = "Create", id= 0 }
);
If referenceID in Create method is optional...you need to change the type of referenceID to int? (nullable int)...as shown below
public ActionResult Create(int? referenceID)
{
// if referenceID is null then you may redirect the user to a view of your choice..
// then what ever logic you may wanna write...
}
Hope this helps...
Update:
Can you try moving this map route code to be your first map route code?
Probably I should have read your question better...in your Create action method you are not returning any model to your view...and when you say Model.referenceId...there should be a model (that contains referenceID as a property) returned from your Create action method...which is missing in your case
Upvotes: 0
Reputation: 57
Based on the code you provided, I'd say your Model is null, because your action doesn't pass anything to the view. Have you tried just showing the ViewBag.id on the page?
Upvotes: 2