0

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; }
ADyson
  • 57,178
  • 14
  • 51
  • 63
Zack Antony Bucci
  • 571
  • 1
  • 12
  • 29
  • Data is passed in the model, not through direct access to the request, route data or whatever. Put an `ID` property on your model and set it – Panagiotis Kanavos May 17 '18 at 14:17
  • Hi thank you for the response. How would I do this? As each mymodel has multiple records in each with their own unique ID. – Zack Antony Bucci May 17 '18 at 14:23
  • Add an `ID` property to `Multipledata` and set it, the same way you set the `projects` property. Access it with `Model.ID`. – Panagiotis Kanavos May 17 '18 at 14:25
  • My multiple data model contains: public IEnumerable projectss { get; set; } public IEnumerable projectnotess { get; set; } So how would I assign an ID to each record that is the actual ID in the database ID field? – Zack Antony Bucci May 17 '18 at 14:26
  • you would make a `public int ID { get; set;}` property on the Projects class I should imagine, and ensure it's populated. If your class is an Entity Framework object, then ensure it's synced properly with your database – ADyson May 17 '18 at 14:30
  • Hi I have done that. But how do I assign the ID from the ID being passed through the ViewProject controller parameters? – Zack Antony Bucci May 17 '18 at 14:31
  • What do you mean? Your view really makes no sense. Are you intending to return a specific project and not a list of projects? The ViewProject method takes in an ID, but then returns a list of projects. Perhaps you need a different ViewModel for this method just to display the project you actually want. – ADyson May 17 '18 at 14:33
  • 1
    @ZackAntonyBucci the comment doesn't make much sense. EF will load the project properties. If your table has an ID field, your `Project` class should have an `ID` property. *You* won't have to fill it, EF will. Your code though loads *all* projects not the project with the `ID` that matches the URL. Are you trying to use the same model for a list view and a detail view perhaps? – Panagiotis Kanavos May 17 '18 at 14:33
  • Hi yes I am trying to list specific information from a project from its ID. But I also want to list information in another table based on the project ID too. This is why I have two models. Is there a way I can just fill a record into the projectss model regarding the ID instead of EVERYTHING in the database? – Zack Antony Bucci May 17 '18 at 14:40
  • I have managed to accomplish passing the ID through the ViewBag method. Is there a better way of doing this however? – Zack Antony Bucci May 17 '18 at 14:45
  • yes just have a viewmodel which contains just one instance of Project and one instance of ProjectNotes (or a list of notes, if there can be many notes per project). And obviously you'd change your DB query to just return the project (and notes) with the given project ID! Your only issue originally is that you return too many Projects. Anyway ViewBag is not meant to be used to supply the main data for the view. – ADyson May 17 '18 at 15:05
  • It seems like this is more of a conceptual issue for you than a technical one. – ADyson May 17 '18 at 15:07
  • Yes I should really fill the projectss model with one record. How would I do this instead of using the ProjectList? is there a way to insert the data just from the specific ID before I send it in the controller? – Zack Antony Bucci May 17 '18 at 15:13
  • like I said you need a totally different ViewModel class, ignore your Multipledata class and make a new one which meets your requirement for this particular view. And to get the specific project from the database you just need to add a .SingleOrDefault() clause to your EF query. – ADyson May 18 '18 at 08:16
  • viewmodel something like `public class ProjectData { public Projects projects { get; set; } public IEnumerable projectnotess { get; set; } }` and query something like `var mymodel = new Projectdata(); mymodel.projects = DB.Projects.SingleOrDefault(p => p.id == id); mymodel.projectnotess = DB.ProjectNotes.Where(pn => pn.ProjectID == id).ToList();` . This is just really about single objects vs. lists of objects. You need to try and get this kind of thing straight in your head or you'll have continual problems. – ADyson May 18 '18 at 08:19
  • Thank you I have now got this working by instead of sending a list of Projects I have sent the specific Project data I have defined in the database model like you have said. – Zack Antony Bucci May 22 '18 at 09:08

1 Answers1

0

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

Zack Antony Bucci
  • 571
  • 1
  • 12
  • 29