1

I am brand new to ASP.NET MVC and I'm really confused. I have a controller ActionResult Method that assigns values to different items from my viewmodel. I've done a lot of searching but I just can't figure it out. How do you get the controller to run when the view loads, and then take a value from the method and use it in your view? In my view I have a span and I want the value of StatusVM.StatusName from the controller method to display in a label in the view.

Here is my view:

@model Models.StatusViewModel

@using (Html.BeginForm())
{    
    <span>@Html.LabelFor(m => m.StatusName)</span>
}

Here is my controller:

public ActionResult Status()
{
    var Statuses = _Methods.GetStatuses();

    var AccountInfo = _Methods.GetAccount(User.AccountID) ?? new TC.DomainModels.Models.AccountModel();

    var StatusVM = new StatusViewModel();            

    //fill out the VM, you can use Mappers, you can use switch, anything to get the values form the account and tracStatusList into the VM

    var accountStatusInfo = Statuses.Where(s => s.StatusId == (int)AccountInfo.Status).FirstOrDefault();

    StatusVM.StatusName = String.IsNullOrEmpty(accountStatusInfo.StatusMask) ? accountStatusInfo.Status : accountStatusInfo.StatusMask;

    var carrierTerm = _accountService.GetAccountCarrierTerms(User.AccountID);
    if(carrierTerm.IsSMSNE)
    {
        StatusVM.ImagePath = accountStatusInfo.SmsneImage; 
    }                
    else
    {
         StatusVM.ImagePath = accountStatusInfo.StatusImage; 
    }


    switch (AccountInfo.Status)
    {
        case TC.DomainModels.Models.Status.Certified :
            StatusVM.ModalPath = "";
            break;
    }

    return PartialView("Navbar/nav_carrierstatus_new", StatusVM);
}

and here is my viewmodel:

public class StatusViewModel
{
    public string StatusName { get; set; }
    public string ImagePath { get; set; }
    public bool UseImage { get { return !String.IsNullOrEmpty(ImagePath); } }
    public string ModalPath { get; set; }

    public StatusViewModel()
    {

    }
}

If anyone can tell me what I'm doing wrong that would be great. Thanks in advance.

Updated Error Message:

{"Execution of the child request failed. Please examine the InnerException for more information."}

{"The controller for path '/' was not found or does not implement IController."}

StackTrace:

   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap[TResult](Func`1 func)
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerAsyncWrapper.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)
   at System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage)
   at System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm)
   at System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm)
   at System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter)
   at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues)
   at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName)
   at ASP._Page_Views_Shared_Navbar__navbar_cshtml.Execute() in c:\source code\TC Web UI Project\Branches\Branch Update TF Status Process\Views\Shared\Navbar\_navbar.cshtml:line 20
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
   at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
   at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName)
   at ASP._Page_Views_Shared_std_siteheader_partialview_cshtml.Execute() in c:\source code\TC Web UI Project\Branches\Branch Update TF Status Process\\Views\Shared\std_siteheader_partialview.cshtml:line 7
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
   at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
   at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName)
   at ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\source code\TC Web UI Project\Branches\Branch Update TF Status Process\Views\Shared\_Layout.cshtml:line 33
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer)
   at System.Web.WebPages.WebPageBase.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer)
   at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
   at System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content)
   at System.Web.WebPages.WebPageBase.Write(HelperResult result)
   at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
   at System.Web.WebPages.WebPageBase.PopContext()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

View that calls partial view:

<nav class="navbar navbar-default">
    <div id="AccountInfoBar" class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#top-nav">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @Html.Partial("Navbar/nav_branding_icons")
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="top-nav">
            <ul class="nav navbar-nav">
                <li id="acctswitch_navsection">@Html.Partial("Navbar/nav_account_switching", new List<Tcetra.Models.AccountSwitchViewModelList>())</li>
                <li id="account_navsection">@Html.Partial("Navbar/nav_account_info")</li>\
                <li id="carriersts_navsection">@Html.Action("Status", "Navbar")</li>
            </ul>

            <ul class="nav navbar-nav navbar-right">
                @Html.Partial("Navbar/nav_recent_transactions")
                @Html.Partial("Navbar/nav_cart")
                <li id="signout"><a href="@Url.Action("Logout", "Login", new { area = "" })" class="HeaderSignOutLink">Sign Out</a></li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
hollyquinn
  • 652
  • 5
  • 15
  • 48
  • 1
    What URL are you using to access this? – Patrick Dec 09 '15 at 17:40
  • 2
    What you're describing doesn't make any sense. The view *won't* "load" unless a controller action responds with it. How are you seeing a view without invoking a controller action? – David Dec 09 '15 at 17:41
  • @Patrick it loads inside of another page. I'm getting it from
  • @Html.Partial("Navbar/TStatus")
  • – hollyquinn Dec 09 '15 at 17:42
  • @David I have no clue. It's a PartialView that loads inside of another View. When it loads it just displays the words TracStatusName – hollyquinn Dec 09 '15 at 17:44
  • What is the name of your controller and where exactly do you plan to get "TStatus" from? – Patrick Dec 09 '15 at 17:45
  • The name of my controller is AccountController. Sorry that was a typo it should be "Navbar/Status. Status is a view that is inside my project inside of a folder called Navbar. – hollyquinn Dec 09 '15 at 17:55
  • You are describing a [Child Action](http://stackoverflow.com/questions/12530016/what-is-an-mvc-child-action). See also http://stackoverflow.com/questions/5248183/html-partial-vs-html-renderpartial-html-action-vs-html-renderaction. – Jasen Dec 09 '15 at 18:00
  • Please post the View that calls this partial or attempts to render the Status controller action. – Greg Burghardt Dec 09 '15 at 19:35
  • @GregBurghardt I added it to the question. – hollyquinn Dec 09 '15 at 19:39
  • 1
    I have to go back to what @Patrick asked you. What URL are you attempting to show in the browser? The exception you posted is referencing the `_layout.cshtml` file. Can you post that? – Greg Burghardt Dec 09 '15 at 19:46
  • 1
    Have a look at `std_siteheader_partialview.cshtml:line 7`. This is referenced in your stack trace. You really should have a closer look at the exception message and stack trace. I think your current error is something else now. – Greg Burghardt Dec 09 '15 at 19:51
  • @GregBurghardt I'll have a look at it. If I change the link back to what I originally had
  • @Html.Partial("Navbar/Status")
  • The error message goes away, but I don't get my data back in my view. I'll look at the pages you mentioned though. – hollyquinn Dec 09 '15 at 19:55
  • 1
    What is the name of the controller for the Status action? Does it live in an "Area" in ASP.NET MVC? It feels like a routing issue in MVC now. – Greg Burghardt Dec 09 '15 at 19:58
  • @GregBurghardt THe name of the controller is AccountController.cs. it is inside the Controllers folder. – hollyquinn Dec 09 '15 at 20:08
  • @GregBurghardt I think the pages themselves live in an area. The Status page and the navbar pages are in views/shared/navbar – hollyquinn Dec 09 '15 at 20:09
  • 1
    No, what is the name of the controller for the **Status** method that you posted in the question? – Greg Burghardt Dec 09 '15 at 20:09
  • @GregBurghardt The name of the controller is AccountController.cs – hollyquinn Dec 09 '15 at 20:11