我是MVC的初学者和新手。我目前正在进行一个演示项目,我很难继续。这是我项目的模型:
public class Deliveries
{
[Key]
public int Id { get; set; }
// public Boolean Checkbox { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Street { get; set; }
public int StreetNumber { get; set; }
public int Zipcode { get; set; }
public string City { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime Date { get; set; }
public TimeSpan Time { get; set; }
public string Details { get; set; }
}
public class Tours
{
[Key]
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual List<int> DeliveriesIds { get; set; }
public virtual List<Deliveries> Deliveries { get; set; }
}
我创建了交付并选择了一对ViewModel:
public class SelectedDeliveriesEditorViewModel
{
public bool Selected { get; set; }
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Street { get; set; }
public int StreetNumber { get; set; }
public int Zipcode { get; set; }
public string City { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime Date { get; set; }
public TimeSpan Time { get; set; }
public string Details { get; set; }
}
public class SelectedDeliveriesWrapperViewModel
{
public List<SelectedDeliveriesEditorViewModel> SelectedDeliveries { get; set; }
public SelectedDeliveriesWrapperViewModel()
{
this.SelectedDeliveries = new List<SelectedDeliveriesEditorViewModel>();
}
public string TourName { get; set; }
public List<int> getSelectedIds()
{
return (from t in this.SelectedDeliveries where t.Selected select t.Id).ToList();
}
}
最后,我想将选定的交付显示为巡视。这是我的ToursController:
public class ToursController : Controller
{
DeliveriesManagerDb _db = new DeliveriesManagerDb();
//
// GET: /Tours/
public ActionResult Index()
{
var model = new SelectedDeliveriesWrapperViewModel();
foreach(var delivery in _db.Deliveries)
{
var editorViewModel = new SelectedDeliveriesEditorViewModel
{
Id = delivery.Id,
FirstName = delivery.FirstName,
LastName = delivery.LastName,
Street = delivery.Street,
StreetNumber = delivery.StreetNumber,
Zipcode = delivery.Zipcode,
City = delivery.City,
Date = delivery.Date,
Time = delivery.Time,
Details = delivery.Details,
Selected = false
};
model.SelectedDeliveries.Add(editorViewModel);
}
return View(model);
}
//
// POST: /Tours/SubmitSelected
[HttpPost]
public ActionResult SubmitSelected(SelectedDeliveriesWrapperViewModel model)
{
var selectedIds = model.getSelectedIds();
Tours tour = new Tours();
tour.Name = model.TourName;
tour.Deliveries = _db.Deliveries.ToList();
tour.DeliveriesIds = selectedIds.ToList();
_db.Tours.Add(tour);
_db.SaveChanges();
return RedirectToAction("TourManagement", "Tours", new { id = tour.Id });
}
[HttpGet]
public ActionResult TourManagement(int id)
{
Tours tour = _db.Tours.Find(id);
if (tour == null)
{
return HttpNotFound();
}
return View(tour);
}
}
当我在ActionResult TourManagemnt中调用View时,我收到NullException错误。当我调试时,我在ActionResult SubmitSelected中看到DeliveriesIds的值,但是当它将它们传递给TourManagement操作时,我将丢失列表为空,只有Tour的名称和所有交付的集合都存在。
对于这篇长篇文章感到抱歉,非常感谢任何帮助。