title: send a Model that is composed of other Model to the view and how to access the attributes
body: <p>I want to send a Model to the view that is composed of another model.
  Problem is i can't access the attributes of the composed Model in view.
  The main Model is <code>FacadeController</code> and is composed of Admin Model.
  the view accepts the Model <code>FacadeController</code> and then i can't access the attributes of the Admin Model like this. 
  " <code>@Model.FacadeController.Admin</code> " </p>
  
  <p>My semester projects depends on it please help.
  Thank you.</p>
  
  <pre><code># this is the Admin Model #
  public class Admins
      {
          public int Admin_Id { get; set; }
          public string UserName { get; set; }
          public string Email { get; set; }
          public string Password { get; set; }
          public string Image_path { get; set; }
          public HttpPostedFileBase Img { get; set; }
  
          //public AdoClass dataClass { get; set; }
          public Admins()
          {
              //dataClass = new AdoClass();
          }
      }
  </code></pre>
  
  <pre><code># this is the facadeController Model #
  namespace EAProject.Models
  {
      public class facadeController
      {
          public Admins Admin { get; set; }
          public facadeController()
          {
              Admin = new Admins();
          }
      }
  }
  </code></pre>
  
  <pre><code>#Controller #
  public class Admin192_168Controller : Controller
      {
          public static facadeController facade;
          // GET: Admin192_168
          public ActionResult Index()
          {
  
              return View();
          }
          [HttpPost]
          public ActionResult Index(Admins currAdmin)
          {
              facade = new facadeController();
              facade.Admin = currAdmin;
  
              if (facade.LoginAdmin(facade.Admin))
              {
                  currAdmin = facade.Admin;
                  Session["AdminObj"] = currAdmin;
                  if (Session["AdminObj"] == currAdmin)
                  {
                      return RedirectToAction("Dashboard", "Admin192_168");
                  }
              }
              return View();
          }
          [HttpGet]
          public ActionResult Dashboard()
          {
              if (Session["AdminObj"] != null)
              {
                  Admins ad = Session["AdminObj"] as Admins;
                  facade.Admin = ad;
                  return View(facade);
              }
              return RedirectToAction("Index", "Admin192_168");
          }
          [HttpGet]
          public ActionResult Profile()
          {
              if (Session["AdminObj"] != null)
              {
                  Admins ad = Session["AdminObj"] as Admins;
                  return View(ad);
              }
              return RedirectToAction("Index", "Admin192_168");
          }
          [HttpPost]
          public new ActionResult Profile(Admins ad)
          {
              if(Session["AdminObj"] != null){
                  Admins temp = Session["AdminObj"] as Admins;
                  ad.Admin_Id = temp.Admin_Id;
                  facade.Admin = ad;
                  string filename = Path.GetFileNameWithoutExtension(facade.Admin.Img.FileName);
                  string extension = Path.GetExtension(facade.Admin.Img.FileName);
                  if(System.IO.File.Exists(temp.Image_path))
                  {
                      System.IO.File.Delete(temp.Image_path);
                  }
                  filename = filename + extension;
                  facade.Admin.Image_path = "~/images/Admin/" + filename;
                  filename = Path.Combine(Server.MapPath("~/images/Admin/"), filename);
                  Response.Write(Server.MapPath("~/images/Admin/" + "DefaultAdmin.PNG"));
                  facade.Admin.Img.SaveAs(filename);
                  if (facade.AdminEditProfile(facade.Admin))
                  {
                      ad = facade.Admin;
                      Session["AdminObj"] = null;
                      if (Session["AdminObj"] == ad)
                      {
                          return RedirectToAction("Dashboard", "Admin192_168");
                      }
                  }
              }
              return RedirectToAction("Index", "Admin192_168");
          }
  
          [HttpGet]
          public ActionResult Logout(bool op)
          {
              Session["AdminObj"] = null;
              return RedirectToAction("Index", "Admin192_168");
          }
  
  
          //This is to 
          /// &lt;Add_the_Page_Dynamically&gt;
          [HttpGet]
          public ActionResult Create_Pages()
          {
              if (Session["AdminObj"] != null)
              {
                  Admins ad = Session["AdminObj"] as Admins;
                  return View(ad);
              }
              return RedirectToAction("Index", "Admin192_168");
          }
          /// &lt;/Add_the_Page_Dynamically&gt;
  
      }
  }
  
  </code></pre>
  
  <pre><code># Dashboard.cshtml view#
  @model EAProject.Models.facadeController
  
  &lt;section class="section"&gt;
      &lt;h1 class="section-header"&gt;
          &lt;div&gt;
              Dashboard
          &lt;/div&gt;
      &lt;/h1&gt;
      &lt;div&gt;
          @Html.Partial("Sajjad",Model.Admin);
      &lt;/div&gt;
  &lt;/section&gt;
  
  </code></pre>
  
  <pre><code># Sajjad.cshtml  Partial View#
  @model EAProject.Models.Admins
  
  &lt;h5&gt;@Model.UserName&lt;/h5&gt;
  &lt;h5&gt;@Model.Email&lt;/h5&gt;
  
  
  </code></pre>
  
  <p>Error and Stack trace</p>
  
  <p>Server Error in '/' Application.
  The model item passed into the dictionary is of type 'EAProject.Models.facadeController', but this dictionary requires a model item of type 'EAProject.Models.Admins'.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>
  
  <p>Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'EAProject.Models.facadeController', but this dictionary requires a model item of type 'EAProject.Models.Admins'.</p>
  
  <p>Source Error: </p>
  
  <p>An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</p>
  
  <p>Stack Trace: </p>
  
  <p>[InvalidOperationException: The model item passed into the dictionary is of type 'EAProject.Models.facadeController', but this dictionary requires a model item of type 'EAProject.Models.Admins'.]
     System.Web.Mvc.ViewDataDictionary<code>1.SetModel(Object value) +175
     System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +107
     System.Web.Mvc.WebViewPage</code>1.SetViewData(ViewDataDictionary viewData) +49
     System.Web.Mvc.WebViewPage.ConfigurePage(WebPageBase parentPage) +57
     System.Web.WebPages.&lt;>c__DisplayClass40_0.b__0(TextWriter writer) +218
     System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +10
     System.Web.WebPages.WebPageBase.Write(HelperResult result) +80
     System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action<code>1 body) +63
     System.Web.WebPages.WebPageBase.PopContext() +237
     System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +98
     System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
     System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
     System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList</code>1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList<code>1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList</code>1 filters, ActionResult actionResult) +52
     System.Web.Mvc.Async.&lt;>c__DisplayClass3_6.b__4() +198
     System.Web.Mvc.Async.&lt;>c__DisplayClass3_1.b__1(IAsyncResult asyncResult) +100
     System.Web.Mvc.Async.WrappedAsyncResult<code>1.CallEndDelegate(IAsyncResult asyncResult) +10
     System.Web.Mvc.Async.WrappedAsyncResultBase</code>1.End() +49
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
     System.Web.Mvc.&lt;>c.b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
     System.Web.Mvc.Async.WrappedAsyncVoid<code>1.CallEndDelegate(IAsyncResult asyncResult) +29
     System.Web.Mvc.Async.WrappedAsyncResultBase</code>1.End() +49
     System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
     System.Web.Mvc.&lt;>c.b__151_2(IAsyncResult asyncResult, Controller controller) +13
     System.Web.Mvc.Async.WrappedAsyncVoid<code>1.CallEndDelegate(IAsyncResult asyncResult) +22
     System.Web.Mvc.Async.WrappedAsyncResultBase</code>1.End() +49
     System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
     System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
     System.Web.Mvc.&lt;>c.b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
     System.Web.Mvc.Async.WrappedAsyncVoid<code>1.CallEndDelegate(IAsyncResult asyncResult) +29
     System.Web.Mvc.Async.WrappedAsyncResultBase</code>1.End() +49
     System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
     System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9874041
     System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +159</p>
  
  <p><code>@Model.Admin.Email</code> it gives error but attributes of admin model has all values but still error</p>