ASP.NET MVC 绑定到视图模型

4

当前情况:我开始学习ASP.net MVC开发,之前从事MVVM应用程序开发,我在试图将视图与视图模型/控制器绑定时遇到了困难。我从一个空项目开始尝试创建模型、视图模型、控制器和视图。启动项目后,我遇到了“500内部服务器错误”,但不知道出了什么问题(输出窗口没有错误信息)。我无法理解视图实际上如何绑定到视图模型(可能是因为我太过于深入地思考了MVVM)。

我目前拥有的:

Startup.cs:

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace WebApplication1
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            app.UseIISPlatformHandler();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Sample}/{action=Index}/{id?}");
            });
        }

        // Entry point for the application.
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }
}

模型:

using System;

namespace WebApplication1.Models
{
    public class SomeModel
    {
        public string Title { get; set; }
        public DateTime Time { get; set; }
    }
}

ViewModel:

using System;
using System.Collections.Generic;
using WebApplication1.Models;

namespace WebApplication1.ViewModels
{
    public class SampleViewModel
    {
        public IList<SomeModel> SomeModels { get; set; }

        public SampleViewModel()
        {
            SomeModels = new List<SomeModel>();
            SomeModels.Add(new SomeModel()
            {
                Time = DateTime.Now,
                Title = "Hallo"
            });
        }
    }
}

控制器:

using Microsoft.AspNet.Mvc;
using WebApplication1.ViewModels;

namespace WebApplication1.Controllers
{
    public class SampleController : Controller
    {
        //
        // GET: /Sample/
        public IActionResult Index()
        {
            return View(new SampleViewModel());
        }
    }
}

查看:

@model WebApplication1.ViewModels.SampleViewModel

<!DOCTYPE html>
<html>
<head>
    <title>Hallo</title>
</head>
<body>
    @foreach (var someModel in Model.SomeModels)
    {
        <div>@someModel.Title</div>
    }
</body>
</html>

我发现很多文章都在谈论模型绑定,但它们只涉及表格和输入。我想要展示一些数据,例如从数据库中获取的数据列表,因此不需要向网站提交任何数据。
有人看到我的示例代码中(可能显而易见的)问题了吗?
该项目基于ASP.net 5 MVC 6使用DNX。
我已经设置了一些断点来查看控制器是否被调用。而且确实是被调用了。我通过调试器仔细检查了几个方法,没有发现任何问题。另外,输出窗口也没有显示任何错误。

1
你尝试过逐步执行代码吗?在 Index() 处设置一个断点,你应该能找到错误发生的地方。也许你甚至没有触发控制器。 - Alexander Derck
1
这是一个非常普遍的错误。你可以尝试设置一些断点(索引方法),看看是否会触发它。你还可以在开发者工具(网络)中检查URL地址。同时,检查Routeconfig类中的方法定义。这些是我现在能想到的要点。 - riteshmeher
1
展示启动项。这可能是一个启动配置问题。您并没有提供太多关于问题的细节。500错误可能是任何东西。您从一个空项目开始,因此有理由认为您可能遗漏了运行应用程序所需的某些内容。 - Nkosi
正如其他人所提到的,麻烦您发布一下 Startup.cs 文件。我有一种倾向认为 MVC 的某些配置可能存在问题,但在看到代码之前无法确定。 - Chris Bohatka
我在事件日志中找不到任何东西。 - Stephan
显示剩余5条评论
1个回答

0

在 GET 方法的结果中缺少视图名称。因此,而不是

return View(new SampleViewModel()); 

它必须是

return View("Sample", new SampleViewModel());

我认为视图和控制器之间的连接纯粹是基于约定的。因此,名为Sample的控制器会在名为Views的文件夹中的名为Sample的子文件夹中搜索名为Sample的视图。

不确定为什么,但它就是这样工作的。


1
MVC实际上使用约定来确定如何定位正确的视图。你说得很接近,但你错过的是框架不仅使用控制器名称,还使用操作的名称。在你的情况下,如果你创建了一个~/Views/Sample/Index.cshtml视图,那么它将被正确地定位和呈现,而无需在return语句中指定视图名称。请注意,Index.cshtml对应于控制器上的Index操作。 - John H

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接