0

I have this datamodel: enter image description here so one Project has many Milestones. What I did is this: when I go to the detail of a specific Project, I can add/create Milestones for it, like in the picture: enter image description here When I click "Create Milestone" I navigate to the View where I can create the milestone for this specific Project, when I click save, it will automatically be saved for this project. Here the HttpPost method:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateMilestone([Bind(Include = "Name,Description,FromDate,DueDate,Finished,ProjectID")] Milestone milestone, int? id)
    {
        if (ModelState.IsValid)
        {
            var forProject = db.Projects.Where(x => x.ID == id).FirstOrDefault();
            if (forProject != null)
            {
                milestone.Project = forProject;
                db.Milestones.Add(milestone);
                db.SaveChanges();
                return RedirectToAction("Details", forProject);
            }
            else
            {
                return HttpNotFound();
            }
        }
        else
        {
            return View(milestone);
        }
    }

Here is a screenshot of the CreateMilestone View, and focus on the url (it's localhost:xxxxx/Projects/CreateMilestone/3002). The id parameter in the CreateMilestone method is for the Project ID, and in the url the id (3002) is also for the project. enter image description here

I'm trying to make the app to navigate to the Details view of that specific Project I just added a milestone, which I do actually! And as you see it works: enter image description here

But: look at the url! Instead of being localhost:xxxxx/Projects/Details/3002 it is: http://localhost:55623/Projects/Details/3002?Milestones=System.Collections.Generic.HashSet%601%5BTheProjectManager.Models.Milestone%5D&Users=System.Collections.Generic.HashSet%601%5BTheProjectManager.Models.ApplicationUser%5D&ProjectName=Testing&Description=Testing%20data

So, how can I make the url be like: localhost:xxxxx/Projects/Details/3002 when I navigate to the details view after adding a new milestone?

UPDATE: the Get Details:

public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Project project = db.Projects.Find(id);
        if (project == null)
        {
            return HttpNotFound();
        }
        return View(project);
    }

and the Get CreateMilestone:

public ActionResult CreateMilestone(int? id)
    {
        var forProject = db.Projects.Where(x => x.ID == id).FirstOrDefault();

        if (forProject != null)
        {
            return View();
        }
        else
        {
            return HttpNotFound();
        }            
    }
Arianit
  • 543
  • 2
  • 8
  • 24

2 Answers2

2

In this line

return RedirectToAction("Details", forProject);

you are redirecting to Details action and as parameter you send "Project" object which is serialized to query string. Instead of full object you can use only id. But you also need to change Details action to accept int as parameter instead of Project class

Rudis
  • 1,177
  • 15
  • 30
  • this is the Details method: public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Project project = db.Projects.Find(id); if (project == null) { return HttpNotFound(); } return View(project); } – Arianit Oct 17 '16 at 17:45
1

Instead of return RedirectToAction("Details",forProject); try this return RedirectToAction("Details", new { id = id });

Tinaira
  • 727
  • 2
  • 7
  • 23