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?
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