Asp.net MVC 绑定到视图模型
Asp.net MVC binding to view model
当前情况: 从 ASP.net MVC 开发开始,从 MVVM 应用程序开发开始,我正在为视图与视图模型/控制器的绑定而苦苦挣扎。我从一个空项目开始,尝试创建模型、视图模型、控制器和视图。启动项目时,我收到“500 内部服务器错误”,但不明白出了什么问题(输出中没有错误 window)。我只是无法理解视图实际上是如何绑定到视图模型的(可能是因为我在 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>
我发现很多文章都在谈论模型绑定,但他们只谈论表单和输入。我想要的是显示一些数据,例如来自某种列表中的数据库,因此不需要 post 网站的任何日期。
有人在我的示例代码中看到(可能很明显的)问题吗?
该项目基于 ASP.net 5 MVC 6 使用 DNX.
我已经设置了一些断点来查看是否真正调用了控制器。它是。我使用调试器完成了几种方法,没有任何问题。此外,输出 window 没有显示任何错误或某事。就这样。
GET 方法的结果中缺少视图名称。所以而不是
return View(new SampleViewModel());
必须是
return View("Sample", new SampleViewModel());
我认为视图和控制器之间的连接纯粹是基于约定的。因此,名为 Sample
的控制器在名为 Sample
的文件夹中搜索名为 Sample
的视图,而该文件夹又是 Views
的子文件夹。
不确定具体原因,但它是这样工作的。
当前情况: 从 ASP.net MVC 开发开始,从 MVVM 应用程序开发开始,我正在为视图与视图模型/控制器的绑定而苦苦挣扎。我从一个空项目开始,尝试创建模型、视图模型、控制器和视图。启动项目时,我收到“500 内部服务器错误”,但不明白出了什么问题(输出中没有错误 window)。我只是无法理解视图实际上是如何绑定到视图模型的(可能是因为我在 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>
我发现很多文章都在谈论模型绑定,但他们只谈论表单和输入。我想要的是显示一些数据,例如来自某种列表中的数据库,因此不需要 post 网站的任何日期。
有人在我的示例代码中看到(可能很明显的)问题吗?
该项目基于 ASP.net 5 MVC 6 使用 DNX.
我已经设置了一些断点来查看是否真正调用了控制器。它是。我使用调试器完成了几种方法,没有任何问题。此外,输出 window 没有显示任何错误或某事。就这样。
GET 方法的结果中缺少视图名称。所以而不是
return View(new SampleViewModel());
必须是
return View("Sample", new SampleViewModel());
我认为视图和控制器之间的连接纯粹是基于约定的。因此,名为 Sample
的控制器在名为 Sample
的文件夹中搜索名为 Sample
的视图,而该文件夹又是 Views
的子文件夹。
不确定具体原因,但它是这样工作的。