2009-04-10 51 views
0

考慮以下情形強類型的視圖:ASP.NET MVC - 用局部視圖(圖和局部的觀點也應該有機會獲得一些全局數據)

操作編輯()轉發到Edit.aspx視圖渲染風景。

Edit.aspx由TextBox1的和兩個部分視圖(又名視圖用戶控件)的: part1.ascx(其具有TextBox2中,textbox3) 和part2.ascx(其具有checkbox1和checkbox2)

你想爲Edit.aspx提供強類型視圖,例如,您使用EditViewData類。

您還需要Edit.aspx,part1.ascx和part2.ascx才能訪問一些全局信息,如currentUserID,currentUserLanguage,currentUserTimezone。

問題:

  1. 你如何去如何組織的EditViewData類?
  2. 如何將視圖數據傳遞給視圖和部分視圖,以便在您提交表單並返回Edit()http.post操作時自動填充對象?
  3. 你傳遞給Edit()http.post動作是什麼?

回答

1

你的可視數據應該是這樣的:

public class EditViewData 
{ 
    public int currentUserID { get; set; } 
    public string currentUserLanguage { get; set; } 
    public string currentUserTimezone { get; set; } 
    // ... other stuff 
} 

後你強烈鍵入ASPX,還需要大力鍵入您ascxs。然後在你的aspx,當你打電話的RenderPartial,只需撥打像往常一樣:

<% using (Html.BeginForm()) %> 
<% Html.RenderPartial("part1.ascx"); %> 
<% Html.RenderPartial("part2.ascx"); %> 
<%}%> 

應該自動繼承在控制模型。只要記住你的BeginForm應該圍繞你的兩個控件(ascxs)。

+0

+1 - 所有這一次,我一直在傳遞this.Model作爲部分的模型參數。沒有意識到它會自動傳遞下來。好一個。 – 2010-09-22 23:55:20