Does Angular routing template url support *.cshtml files in ASP.Net MVC 5 Project?

后端 未结 4 1561
感情败类
感情败类 2020-11-30 03:03

I am working on a MVC 5 project. When I use a html page at my views, it load that page but when I use .cshtml page it is not loading the view. The Blank page appears.

<
4条回答
  •  鱼传尺愫
    2020-11-30 03:48

    Yes, you can.

    Adding a similar answer to Yasser's, but using ngRoute:

    1) Instead of referencing your partial HTML, you need to reference a Controller/Action to your ASP.NET MVC app.

    .when('/order', {
        templateUrl: '/Order/Create',
        controller: 'ngOrderController' // Angular Controller
    })
    

    2) Your ASP.NET MVC will return a .cshtml view:

    public class OrderController : Controller
    {
        public ActionResult Create()
        {
            var model = new MyModel();
            model.Test= "xyz";
    
            return View("MyView", model);
        }
    }
    

    3) Your MyView.cshtml will mix Razor and Angular. Attention: as its a partial for your Angular app, set the layout as null.

    @model MyProject.MyModel
    
    @{
       Layout = null;
    }
    
    

    {{ Test }}

    Model.Test

提交回复
热议问题