How to instanciate a class when its property members are not of primitive types?
Posted
by Richard77
on Stack Overflow
See other posts from Stack Overflow
or by Richard77
Published on 2010-04-18T08:34:29Z
Indexed on
2010/04/18
8:43 UTC
Read the original article
Hit count: 307
asp.net-mvc
|c#
Hello,
1) Let's say I've a class MyDataInfo
public class MyDataInfo
{
public int MyDataInfoID { get; set; }
public string Name { get; set; }
}
For the purpose of the fuctionality I'm after, I've created another class (MyData) whose property members are of MyDataInfo type.
2) Here's myData
public class MyData
{
public MyDataInfo Prop1 { get; set; }
public MyDataInfo Prop2 { get; set; }
}
3) And, here's my action method
public ActionResult MyAction()
{
MyData myObject = new MyData();
return View(myObject);
}
4) Finally, this in my View template (which is strongly typed and inherits from MyData)
<% = Html.Encode (Model.Prop1.Name)%>
<% = Html.Encode (Model.Prop2.Name)%>
Unfortunately, I got an error "Object not set to an instance of an object."
Am I missing something or is there a different way of obtaining the same result?
Thanks for helping
© Stack Overflow or respective owner