Hi I am trying to pass an ID from the URL from a controller to a view. I am also passing a viewmodel that contains two models that have data from a database. Is there another way of doing this? The routemap has been set to send an ID.
The URL would be http://localhost:55147/Home/ViewProject/8
which obviously 8 is the ID of the project, but it does not seem to get this. I am passing the ID to the ViewProject controller function by URL.Action which has the ID passed through the parameter on another page.
Here is my controller:
public ActionResult ViewProject( int? id )
{
if( id == null )
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
BextecODBEntities DB = new BextecODBEntities();
var mymodel = new Multipledata();
mymodel.projectss = DB.Projects.ToList();
mymodel.projectnotess = DB.ProjectNotes.ToList();
return View("ViewProject", mymodel);
}
Here is my View:
@foreach(var item in Model.projectss)
{
if( item.ID.ToString() == Html.ViewContext.RouteData.Values["id"].ToString() )
{
@Html.DisplayText("You have clicked on Project ID:" + item.ID.ToString())
}
}
My multiple data model contains:
public IEnumerable<Projects> projectss { get; set; }
public IEnumerable<ProjectNotes> projectnotess { get; set; }
Instead of sending a list of Projects I have sent the specific Project data I have defined in the database model using the Project.Find(id) passed through the parameter like you have said