【发布时间】:2014-03-26 14:47:47
【问题描述】:
我在一个常见问题中使用了这个答案https://stackoverflow.com/a/11682510/801005。 当您想立即将复杂类型添加到列表中时,这适用于编辑数据,但不适用于创建数据。
如果您在控制器中进行创建操作,您应该创建AssignSoftwareLicenseViewModel 的实例并创建ICollection<SelectableDeviceViewModel> Devices 的实例。之后,您必须创建SelectableDeviceViewModel 的实例并将其添加到集合中。然后将创建的AssignSoftwareLicenseViewModel返回给视图。
所有文本框都创建得很好,帖子也能正常工作。但是因为您创建了对象的实例,所以LicenseId 在您的屏幕上具有值0。如果您还有 DateTime 属性,您将在屏幕上的文本框中获得 01-01-0001 作为默认值。
如何删除这些值?我的显示属性现在没用了……
示例日期时间:
[Display(Name = "Geboortedatum")]
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime BirthDay { get; set; }
示例创建
public ActionResult Create()
{
AssignSoftwareLicenseViewModel vm = new AssignSoftwareLicenseViewModel();
vm.Devices = new Collection<SelectableDeviceViewModel>();
vm.Devices.Add(new SelectableDeviceViewModel());
return View(vm);
}
【问题讨论】:
标签: c# asp.net-mvc