0

I've been new to ASP.NET MVC. This is what I'm doing. I've 2 Controllers:Home and Customerservice. Now I have a Customer list where when I click details gets redirected to the products he acquired. So, I need to pass in the id so that the products of that customer can be displayed. So, my home consists of customer details. Now i need to pass that id to CustomerService controller ,Index action. This is what I've done in Home:

  public ActionResult Customers()
    {
        var dc = new ServicesDataContext();
        var query = (from m in dc.Customers
                     select m);


        return View(query);
    }

    public ActionResult Details(int id)
    {
        var datacontext = new ServicesDataContext();
        var serviceToUpdate = datacontext.Customers.First(m => m.CustomerId == id);


        ViewData.Model = serviceToUpdate;
       // return View();
        return Redirect("/CustomerService");

    }
    [HttpPost]
    public ActionResult Details(FormCollection form)
    {
        var id = Int32.Parse(form["CustomerID"]);
        var datacontext = new ServicesDataContext();
        var service = datacontext.Customers.First(m => m.CustomerId == id);
        return Redirect("Customers");

    }
}

Now I'm not sure whether I need to pass an id as parameter for index in CustomerService. SO can you please guide me in finishing this?

2 Answers 2

2

If you are using any Redirect (such as RedirectToAction) you can use TempData to store any parameters. The semantics have slightly changed in MVC 3 but TempData is designed to pass data between actions in a POST-Redirect-GET scenario.

Sign up to request clarification or add additional context in comments.

Comments

1

Passing it as a parameter is probably your best option. Try using something like return RedirectToAction(ActionName, ControllerName, RouteValues);.

6 Comments

I have one more doubt : route values is nothing but the parameter right?
If that's all you need, then yeah. So return RedirectToAction(ActionName, ControllerName, new{id=id});
I'm getting url?Length=11. I don't know what is that length
I'm not getting as per customers .... Do i need to change index method of Customerservice controller?
I have no idea. It's kinda hard to comment on code I can't see, given you only provided the home controller, and haven't shown your updated code either.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.