2016-07-11 3 views
0

ているモデルでHtml.RenderAction結果を使用してコントローラにビューからモデルを渡す私はモデルを次ていますはヌル

public class Foo 
{ 
    public List<Employee> Employees{ get; set; } 
    public List<Company> Companies{ get; set; } 
    public List<Admin> Admins{ get; set; } 
} 

それから私は私のコントローラのアクションを持っている:

public ActionResult Index() 
{ 
    Foo model = GetFooFromSomewhere(); 
    return PartialView("Index", model); 
} 

public ActionResult Employees(List<Employee> model) 
{ 
    return PartialView("Employees", model); 
} 

public ActionResult Companies(List<Company> model) 
{ 
    return PartialView("Companies", model); 
} 

public ActionResult Admins(List<Admin> model) 
{ 
    return PartialView("Admins", model); 
} 

は、その後、私は自分の意見を持っている

Index.cshml:

@model Foo 
@if(Model.Employees.Count > 0) 
{ 
    @{Html.RenderAction("Employees", "Config", Model.Employees);} 
} 
@if(Model.Companies.Count > 0) 
{ 
    @{Html.RenderAction("Companies", "Config", Model.Companies);} 
} 
@if(Model.Admins.Count > 0) 
{ 
    @{Html.RenderAction("Admins", "Config", Model.Admins);} 
} 

Employees.cshtml:

@model List<Employee> 

//Display model here 

Companies.cshtml

@model List<Company> 

//Display model here 

Admins.cshtml

@model List<Admin> 

//Display model here 

あなたが見ることができるように、私はIndex.cshtmlが複数含まれているオブジェクトを取得するために使用リスト。これは、リストにアイテムが見つからない場合にアクションを非表示にする必要があるためです。しかし、@ Html.RenderAction(...)を使ってコントローラーに再度渡すと、Listを待っているときにコントローラーのアクション内でnullが返されます。どうして?

答えて

2

このようにしてみてください。 コントローラ:

public ActionResult Admins(List<Admin> m) 
{ 
    return PartialView("Admins", m); 
} 

ビュー:

@{Html.RenderAction("Admins", "Config", new { m = Model.Admins });} 
+0

感謝 – LeonidasFett

+0

素晴らしい!あなたを助けてうれしい! – Luca

1

コントローラのIndexビューに初期化されたモデルを渡す必要があります。

public ActionResult Index() 
{ 
    Foo model = GetFooFromSomewhere(); 
    return PartialView("Index", model); 
} 
+0

私はタイプミスでしたごめんなさい:)これが問題だった – LeonidasFett