2012-06-25 55 views
0

我是ASP.NET MVC3的新手。我使用Model First方法在ASP.NET MVC3中創建了一個項目。
我有以下實體:CustomerCall如何使用創建視圖用於不同的目的?

這些實體之間的關係是一個(客戶)有很多(通話)。我爲這兩個實體創建了控制器。

問題是,在客戶的索引視圖中,我添加了ActionLink以添加對特定客戶的呼叫。代碼如下:

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

點擊此鏈接後,它打開創建呼叫視圖。在通話創建視圖中,我想顯示特定客戶的名稱。
如何使用傳遞的CustomerId?這個怎麼做?

+0

我會爲此場景創建新動作,例如'CreateForCustomer(int id)'具有單獨的視圖(部分對於彈出窗口來說是完美的)。你可以用'RouteData'來提取控制器中的「id」值,然後從orm獲取客戶信息,然後將它傳遞給視圖(即用'ViewBag')。問題是,在視圖中,你將不得不編寫大量的剃鬚刀代碼來爲兩種不同的場景生成html,並且可能會變得混亂。 – lucask

回答

1

在你CallController改變Create行動接受id參數:

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(); 
} 

在視圖中可以顯示的名稱,根據不同的方法,如:

@Model.CustomerName 

@ViewBag.CustomerName 
相關問題