Asp.net-Mvc

包含cshtml頁面的角度

  • December 27, 2013

我與 Angular 作鬥爭以使其 ng-include ng-view 之外的部分 cshtml 視圖。

這是我的主要觀點:

<div class="container">
   <div class="header">
       <div class="loginView">
           <div ng-include="Home/Template/login"></div>
       </div>
   </div>
</div>

<div class="container">
   <div ng-view></div>
</div>

這是我的部分 Login.cshtml:

<form name="login" ng-controller="LoginCtrl" class="form-inline">
   <div class="form-group"><input type="text" name="loginName" class="form-control input-sm" placeholder="Brugernavn" oninvalid="this.setCustomValidity('Indtast brugernavn')" ng-model="UserData.LoginName" ng-required="true"></div>
   <div class="form-group"><input type="text" name="password" class="form-control input-sm" placeholder="Kodeord" oninvalid="this.setCustomValidity('Indtast kodeord')" ng-model="UserData.Password" ng-required="true"></div>
   <div class="form-group">
       <button class="btn btn-primary btn-sm" ng-click="Login()" ng-disabled="login.password.$error.required || login.loginName.$error.required">Login</button>
   </div>
</form>

我使用 mvc 返回對 url 的呼叫的特定部分視圖,如下所示:

  public ActionResult Template(string id)
   {
       switch (id.ToLower())
       {
           case "login":
               return PartialView("~/Views/Account/Partials/Login.cshtml");
           case "register":
               return PartialView("~/Views/Account/Partials/Register.cshtml");
           case "main":
               return PartialView("~/Views/Main/MainPage.cshtml");
           default:
               throw new Exception("template not known");
       }
   }

基本上我會相信 ng-include 應該呼叫 url 它將返回 html 將包含在頁面上。但事實並非如此,模板方法根本沒有被呼叫。我當然可以在母版頁上包含整個模板,但稍後登錄後我想用登錄的使用者數據模板替換這個模板。

知道如何解決這個問題嗎?

ng-include 評估一個表達式,確保如果您要直接載入路徑,則使用兩組引號。

<div ng-include="'Home/Template/login'"></div>

引用自:https://stackoverflow.com/questions/20801780