繁体   English   中英

在NHaml中使用强类型视图?

[英]Strongly typed views in NHaml?

我有一个强类型的视图,想在NHaml页面中使用它。

使用WebForms引擎,我将在<%@ Page%>指令或代码隐藏文件中描述ViewData类型。

在NHaml中我将如何处理?

页面上有一个补丁(搜索NHaml)来执行此操作。 我不知道是否行得通。 这需要MvcContrib提供的NHaml。

用于NHaml View Engine的补丁程序,可以对其进行升级以与MVC Preview 3一起使用。NHamlView的模型属性包括对接口类型的接口数据是非通用的,因此可以对ViewDataDictionary中的模型数据进行强类型访问,并且我们希望在Views中强类型化对ViewData的访问。 ..例如,预览2下的ViewData.Property将变为预览3下的Model.Property。2008年5月30日应用:在修订版375中应用。

鲍里斯

如果我理解正确,那么您只是想拥有一个强类型的nhaml视图?

在这种情况下,svn中有一个示例项目可以执行此操作。 看一下

http://nhaml.googlecode.com/svn/trunk/src和NHaml.Samples.Mvc.CSharp项目

这是一些提取的代码

调节器

public class ProductsController : Controller
{
    private readonly NorthwindDataContext northwind = new NorthwindDataContext(
        ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString );


    public ActionResult Edit( int id )
    {
        var viewData = new ProductsEditViewData { Product = northwind.GetProductById( id ) };

        viewData.Categories = new SelectList( northwind.GetCategories(), "CategoryID", "CategoryName", viewData.Product.CategoryID );
        viewData.Suppliers = new SelectList( northwind.GetSuppliers(), "SupplierID", "CompanyName", viewData.Product.SupplierID );

        return View( "Edit", viewData );
    }

}

视图

%h2= ViewData.Model.Product.ProductName
%form{action='#{Url.Action("Update", new { ID=ViewData.Model.Product.ProductID \})}' method="post"}
  %table
    %tr
      %td Name:
      %td= Html.TextBox("ProductName", ViewData.Model.Product.ProductName)
    %tr
      %td Category:
      %td= Html.DropDownList("CategoryID", ViewData.Model.Categories, (string)null)
    %tr
      %td Supplier:
      %td= Html.DropDownList("SupplierID", ViewData.Model.Suppliers, (string)null)
    %tr
      %td Unit Price:
      %td= Html.TextBox("UnitPrice", ViewData.Model.Product.UnitPrice.ToString())
  %p
  - Html.RenderPartial(@"_Button")

查看模型

public class ProductsEditViewData
{
    public Product Product { get; set; }
    public SelectList Suppliers { get; set; }
    public SelectList Categories { get; set; }
}

希望能有所帮助

我将在<%@ Page%>指令或代码隐藏文件中描述ViewData类型。

在NHaml中我将如何处理?

不需要这样做 您可以直接使用Model,而无需指定其类型,即可使用 例如:

%h2= Model.PageTitle
  %p= Model.UserMessageOrSomething

这是因为NHAML视图已编译。 因此,当模型上的所有属性正确(名称,类型等)时,将编译视图(如源代码一样)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM