部分视图未加载一次强类型

时间:2014-06-26 21:44:57

标签: ajax asp.net-mvc-5 partial-views strongly-typed-view

控制器:

namespace AdminPortal.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult ServersTab(AdminPortalModel model)
        {      
            return PartialView("~/Views/Home/ServersTab.cshtml", model.Servers);
        }

        public PartialViewResult _ServerNew()
       {
            return PartialView("~/Views/Home/_ServerNew.cshtml");
        }
    }
}

要加载的视图:

@using AdminPortal.CustomHelpers;

@model AdminPortal.Models.AdminPortalModel;

<div id="scrollBox">
This 'div' element uses 'overflow:scroll' to create scrollbars whenever the contents of     the 'div' become too large.
</div>

查看正在加载:

@using AdminPortal.CustomHelpers;

@model IEnumerable<AdminPortal.Models.Server>

...


<div id="NewAndEditButtonWrapper">
    @Ajax.ImageActionLink("../../Images/edit_grey.png", "Edit ==>", "_ServerEdit", "",
        new AjaxOptions
           {
               HttpMethod = "GET",
               InsertionMode = InsertionMode.Replace,
               UpdateTargetId = "NewEditSubview"
           },
                new
                {
                    id = "EditButton"
                })
    </div>
<div id="NewEditSubview">
</div>

自定义Ajax助手:

public static IHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText, string actionName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes = null)
    {
        var builder = new TagBuilder("img");
        builder.MergeAttribute("src", imageUrl);
        builder.MergeAttribute("alt", altText);
        builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions).ToHtmlString();
        return MvcHtmlString.Create(link.Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing)));
    }

基本上,当我的“要加载的视图”包含@model AdminPortal.Models.AdminPortalModel;它没有加载,但当我删除这一行时,它将加载。我需要这个模态,以便我可以在我的视图中使用它来向用户显示一些信息。我不确定为什么强烈键入视图导致View无法加载。有什么建议吗?

修改

进一步扩展“不加载”: When the View is strongly typed

When the View was not strongly typed

1 个答案:

答案 0 :(得分:0)

  

这些错误无法捕捉,因为VS从未准确地为我显示错误,尤其是在.cshtml文件中

您可以在ASP.NET MVC项目中启用视图编译。默认情况下禁用此功能。该属性是MvcBuildViews,需要在项目文件中手动修改。请参阅答案:ASP.Net MVC - Compiler Errors in Views