asp.net-mvc - Angular ng-include cshtml 页面

标签 asp.net-mvc angularjs razor

我正在与 Angular 进行斗争,以使其 ng-include ng-view 之外的部分 cshtml View 。

这是我的主要观点:

<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 时返回特定的部分 View ,如下所示:

   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,该 html 将包含在页面上。但事实并非如此,模板方法根本没有被调用。我当然可以在主页上包含整个模板,但稍后登录后我想用登录的用户数据模板替换该模板。

知道如何解决这个问题吗?

最佳答案

ng-include 计算表达式,请确保如果您要直接加载路径,请使用两组引号。

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

关于asp.net-mvc - Angular ng-include cshtml 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20801780/

相关文章:

javascript - 将 Javascript 中的值返回到 C# HTML 文件

css - 无法更改文本框的大小

css - 为什么这些卡片无法正常显示?

javascript - Angular UI 网格日期格式不正确

javascript - 使用 templateUrl 通过 angular 的 ui-router 加载 html 文件

asp.net-mvc - 如何在 Html.RenderAction (MVC3) 中发送模型对象

c# - 如何编写像 Html.TextBoxFor 这样的自定义 ASP.NET MVC HTML Helper 并设置正确的名称属性

asp.net-mvc - ASP.NET 网络表单是否被扫地出门为 mvc 腾出空间?

jquery - mvc ajax 中的注销 View

c# - 如何使用 ASP.NET MVC Core 初始设置角色和用户(如站点管理员)?