In my controller I do initialization like this:
using mylib.product;
using mylib.factory;
product p = new product();
factory f = new factory(p);
How do I do the same thing using the @model keyword in a partial view?
Thanks
In my controller I do initialization like this:
using mylib.product;
using mylib.factory;
product p = new product();
factory f = new factory(p);
How do I do the same thing using the @model keyword in a partial view?
Thanks
If you are trying to add namespaces/classes to you view, then it's:
@using mylib.product;
I should parse the model to the view by
return View("ViewName");
and in the view;
@model Project.Namespace.Class
You should use view models:
public class MyViewModel
{
public string Name { get; set; }
public string Address { get; set; }
}
which will be passed to the view from the controller action:
public ActionResult Index()
{
product p = new product();
factory f = new factory(p);
var model = new MyViewModel
{
Name = p.Name,
Address = f.Address
}
}
and then your view will be strongly typed to this view model:
@model MyViewModel
@Html.DisplayFor(x => x.Name)
@Html.DisplayFor(x => x.Address)
I think you need to transfer more than one instance of different classes to View.(Am I right?) If yes, I suggest to use ViewBag for it. Something like this:
// Controller
=========
product p = new product();
factory f = new factory(p);
....
// Add some value for p and f
ViewBag.Product = p;
ViewBag.Factory = f;
return View();
// View
=========
var p = (product) ViewBag.Product;
var f = (factory) ViewBag.Factory;
// now you have access to p and f properties, for example:
@Html.Label(p.Name)
@Html.Label(f.Address)
Do not forgot that ViewBag is a Dynamic container and you need to Cast it to a type when you want to use its value in View