我开始学习asp.net MVC,我对将数据发送到VIEW感到困惑。我有一个课程派遣:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Logistic.Models
{
public class Dispatch
{
public int DispatchId { get; set; }
public int TrackingId { get; set; }
public int CustomerId { get; set; }
public int RecipientId { get; set; }
public int CityId { get; set; }
public DateTime Delivered { get; set; }
public string Notes { get; set; }
}
}
另一个班级城市:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Logistic.Models
{
public class City
{
public int CityId { get; set; }
public string Name { get; set; }
}
}
班级客户:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Logistic.Models
{
public class Client
{
public int ClientId { get; set; }
public string Name { get; set; }
}
}
从我的Dispatch类中,CustomerId和RecipientId将与类Client相关; CityId与City相关。
我创建了我的上下文类:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
namespace Logistic.Models
{
public class LogisticContext : DbContext
{
public DbSet<Dispatch> Dispatches { get; set; }
public DbSet<Client> Clients { get; set; }
public DbSet<Driver> Drivers { get; set; }
public DbSet<City> Cities { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
这是我的控制器:
using Logistic.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Logistic.Controllers
{
public class HomeController : Controller
{
private LogisticContext db = new LogisticContext();
public ActionResult Dispatch()
{
var dispatches = db.Dispatches.ToList();
return View(dispatches);
}
}
}
问题是在我的View类中,我不知道如何显示客户名称,收件人姓名和城市名称。我只能显示CustomerId,RecipientId和CityId。
查看:
@model IEnumerable<Logistic.Models.Dispatch>
@{
ViewBag.Title = "Dispatch";
}
<h2>Dispatch</h2>
@foreach(var item in Model)
{
<h2></h2>
}
由于
答案 0 :(得分:0)
如果您已建立关系......
如果您已经在这些属性之间定义了关系,则通常可以使用// Pull all of the dispatches and their related cities
var dispatchesWithCities = db.Dispatches.Include("Cities")
.ToList();
方法提取相关实体,如下所示:
item.City.Name
如果你这样做并且拥有适当的关系,那么你就可以迭代并访问public class DispatchViewModel
{
public Dispatch Dispatch { get; set; }
public City City { get; set; }
public Client Client { get; set; }
public Driver Driver { get; set; }
}
,但是在你当前的场景中看起来并不像这样。
没有关系......
如果你没有这些“硬”关系,你仍然可以执行一些数据库查询来提取你需要的属性并构建一个ViewModel,但它不会有如此多的结果:
// Build your Dispatches with related information
var dispatchModels = db.Dispatches.AsEnumerable()
.Select(d => new DispatchViewModel()
{
Dispatch = d,
City = db.Cities.FirstOrDefault(c => c.CityId == d.CityId),
Client = db.Clients.FirstOrDefault(c => c.ClientId == d.CustomerId),
...
});
然后使用每个Dispatch对象并将它们映射到相关属性:
@foreach(var item in Model)
{
<h2>@item.Dispatch.DispatchId</h2>
<ul>
<li>
<b>City</b>: @item.City.Name
<!-- Other stuff here -->
</li>
</ul>
}
此时,您可以简单地遍历集合并通过引用循环中的迭代器来输出它们:
@Bean
public ManagementHelper getManagementHelperInstance() {
return new ManagementHelper();
}
答案 1 :(得分:0)
您可以使用视图模型方法将数据从多个视图发送到控制器。这是一个例子: http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp-net-mvc-applications/