NHAML强烈打字?
-
22-09-2019 - |
题
我有一个强烈键入的视图,并希望在NHAML页面中使用它。
使用WebForms引擎,我将在 <%@ Page%>
指令或在CodeBehind文件中。
我将如何在Nhaml做到这一点?
解决方案
鲍里斯
如果我正确理解,您只想拥有强烈的键入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; }
}
希望有帮助
其他提示
上 这个 页面有一个补丁程序(搜索NHAML)可以做到这一点。我不知道它是否有效。这需要MVCCONTRIB的NHAML。
NHAML视图引擎的补丁升级以与MVC Preview 3一起使用,包括Nhamlview的模型属性,以允许在ViewDatectionary中强烈键入对模型数据的强烈键入,因为接口属性是非通用的,我们希望在视图中强烈键入视图访问的视图。
我将在<%@ page%>指令或CodeBehind文件中描述ViewData类型。
我将如何在Nhaml做到这一点?
你 不需要这样做. 。你可以 只需使用模型 不指定其类型,它将起作用。例如:
%h2= Model.PageTitle
%p= Model.UserMessageOrSomething
这是因为NHAML视图被编译了。因此,当模型上的所有属性正确(名称,类型等)时,将编译视图(如源代码所需)。
不隶属于 StackOverflow