NHaml 中的强类型视图?

发布于 2024-08-12 03:24:48 字数 142 浏览 7 评论 0原文

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

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

在 NHaml 中我该如何处理呢?

I have a strongly typed view and want to use it in an NHaml page.

With the WebForms engine I would describe the ViewData type in the <%@ Page%> directive or in the codebehind file.

How would I go about that in NHaml?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过期以后 2024-08-19 03:24:48

Boris

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

如果是这种情况,svn 中有一个示例项目可以执行此操作。请查看

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

这是一些提取的代码

Controller

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

}

View

%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")

View Model

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

希望有帮助

Boris

If I understand correctly you just want to have a strong typed nhaml view?

If this is the case there is a sample project in svn that does this. Have a look at

http://nhaml.googlecode.com/svn/trunk/src and the NHaml.Samples.Mvc.CSharp project

And here is some extracted code

Controller

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

}

View

%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")

View Model

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

Hope that helps

蘸点软妹酱 2024-08-19 03:24:48

页面上有一个补丁(搜索 NHaml)可以执行此操作。我不知道它是否有效。这需要来自 MvcContrib 的 NHaml。

NHaml View Engine 升级补丁
它可以与包含的 MVC Preview 3 一起使用
NHamlView 上的 Model 属性
允许强类型访问
ViewDataDictionary 中的模型数据
因为接口属性是非
通用,我们喜欢强类型
我们在视图中的 ViewData 访问..
例如预览 2 下的 ViewData.Property
将成为 Model.Property 下
预览 3 已应用 2008 年 5 月 30 日:已应用
在修订版 375 中。

On this page there's a patch (search for NHaml) to do this. I don't know if it works. This requires NHaml from MvcContrib.

Patch for NHaml View Engine to upgrade
it to work with MVC Preview 3 Included
a Model property on NHamlView's to
allow strongly typed access to the
model data in the ViewDataDictionary
as the interface property is non
generic and we like to strongly type
our ViewData access within Views ..
e.g. ViewData.Property under Preview 2
would become Model.Property under
Preview 3 Applied May 30 2008: Applied
in revision 375.

弱骨蛰伏 2024-08-19 03:24:48

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

我将如何在 NHaml 中解决这个问题?

不需要这样做。您可以仅使用模型而不指定其类型,它就会起作用。例如:

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

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

I would describe the ViewData type in
the <%@ Page%> directive or in the
codebehind file.

How would I go about that in NHaml?

You don't need to do it. You can just use the Model without specifying its type and it will work. For example:

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

This is because the NHAML view gets compiled. So when all the properties on the Model are correct (names, types etc) the view will be compiled (as source code would).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文