0

I'm new to ASP.NET MVC3. I have created one project in ASP.NET MVC3 using Model First approach.
I'm having following entities: Customer and Call

Relation between these entities are one (Customer) has many (Calls). I created controllers for both of those entities.

The issue is that on the Customer's index view I added ActionLink for adding a call for particular customer. Code for this is as follows:

@Html.ActionLink("+Call", "Create", "Call", new { id = item.CustomerId }, null)

After clicking on this link it is opening create view of call. On the Call create view I want to show name of particular customer.
How to use the passed CustomerId? How to do this?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Priyanka
  • 2,802
  • 14
  • 55
  • 88
  • I would create new action for this scenario e.g. `CreateForCustomer(int id)` with separate view (partial would be perfect for popup window). You can do it your way using `RouteData` to extract "id" value in controller, then get customer info from orm and then pass it to the view (i.e. with `ViewBag`). Problem is that in the view you would have to write a lot of razor code to generate html for two different scenarios and it might get messy. – lucask Jun 25 '12 at 11:57

1 Answers1

1

In your CallController change the Create action to accept id parameter:

public ActionResult Create(int id)
{
    // TODO: Query the database to get the customer and his name

    // If you use a ViewModel extend it to include the name of the customer
    // Example: viewModel.CustomerName = retrievedCustomer.Name;

    // Or you can pass it in the ViewBag
    // Example: ViewBag.CustomerName = retrievedCustomer.Name;

    return View(viewModel); // or return View();
}

In the view you can display the name, depending on the approach, as:

@Model.CustomerName

or

@ViewBag.CustomerName
shizik
  • 910
  • 6
  • 16