我是ASP.NET MVC的新手,我真的很困惑。我有一个控制器ActionResult方法,它为我的viewmodel中的不同项分配值。我做了很多搜索,但我无法理解。如何在视图加载时让控制器运行,然后从方法中获取值并在视图中使用它?在我看来,我有一个span,我希望控制器方法中的StatusVM.StatusName的值显示在视图的标签中。
以下是我的观点:
@model Models.StatusViewModel
@using (Html.BeginForm())
{
<span>@Html.LabelFor(m => m.StatusName)</span>
}
这是我的控制器:
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);
}
这是我的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()
{
}
}
如果有人能告诉我我做错了什么就会很棒。提前谢谢。
更新了错误消息:
{"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)
查看调用部分视图:
<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>
答案 0 :(得分:2)
我想我知道你在找什么。从评论到这个问题:
帕特里克说:霍利昆回应道:您使用什么网址来访问它?
@Patrick它加载到另一个页面内。我是从
获取的<li id="carriersts_navsection">@Html.Partial("Navbar/TStatus")</li>
@Html.Partial
方法在执行当前控制器操作期间呈现部分。它不会调用另一个控制器操作 - 我认为这是你的断开连接所在。
相反,看起来你想在渲染当前控制器动作的视图时调用另一个控制器动作:
<li id="carriersts_navsection">@Html.Action("Status", "Navbar")</li>
您需要@Html.Action (ChildActionExtensions.Action)
使用指定的参数和控制器名称调用指定的子操作方法,并将结果作为HTML字符串返回。