2009-04-10 52 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