1
这可能是我遇到的最令人沮丧的错误。没有为此对象定义的无参数构造函数。当处理MultiSelectList
我有一个实体频道与广告位有很多关系。在创建时,用户可以选择他们想要属于他们的频道的广告插槽。
我有这样的模式:
[Required]
[Display(Name = "Name")]
public string ChannelName { get; set; }
[Display(Name = "Description")]
public string ChannelDescription { get; set; }
//TODO: Add Sub Publisher Drop-Down
[Required(ErrorMessage = "You must select at least one Ad Slot.")]
[Display(Name = "Ad Slot(s)")]
public MultiSelectList AdSlots { get; set; }
,这在我看来:
<div class="field">
@Html.LabelFor(m => m.ChannelName)
@Html.TextBoxFor(m => m.ChannelName)
@Html.ValidationMessageFor(m => m.ChannelName)
</div>
<div class="field">
@Html.LabelFor(m => m.ChannelDescription)
@Html.TextAreaFor(m => m.ChannelDescription, new { @class = "description" })
@Html.ValidationMessageFor(m => m.ChannelDescription)
</div>
<div class="field">
@Html.LabelFor(m => m.AdSlots)
@Html.ListBoxFor(m => m.AdSlots, new MultiSelectList(new[] { new { Value = "", Text = "" } }, "Value", "Text", new[] { "" }), new { Class = "multiselect", Multiple = "multiple", Size = 12 })
@Html.ValidationMessageFor(m => m.AdSlots)
</div>
这是处理处理我的控制器操作:
//GET: /publishers/channels/new
public ActionResult New()
{
return View();
}
//POST: /publishers/channels/new
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult New(Models.Channels.Create channelModel)
{
if (ModelState.IsValid)
{
// Channel
Core.Linq.APMaster.Channel channel = new Core.Linq.APMaster.Channel();
channel.PublisherId = PublisherId;
channel.Guid = Guid.NewGuid();
channel.ChannelName = channelModel.ChannelName;
channel.ChannelDescription = channelModel.ChannelDescription;
channel.DateCreated = DateTime.UtcNow;
channel.UserCreated = PublisherId;
//TODO: Fix CreatedFromIP data-type bug.
channel.CreatedFromIP = 0;
channel.IsActive = true;
channel.IsCustom = false;
myDBContext.APMasterDBC.Channels.InsertOnSubmit(channel);
// Ad Slots
foreach (SelectListItem adSlotModel in channelModel.AdSlots)
{
Core.Linq.APMaster.ChannelAdSlot channelAdSlot = new Core.Linq.APMaster.ChannelAdSlot();
channelAdSlot.ChannelId = channel.ChannelId;
channelAdSlot.AdSlotId = int.Parse(adSlotModel.Value);
channelAdSlot.IsActive = true;
myDBContext.APMasterDBC.ChannelAdSlots.InsertOnSubmit(channelAdSlot);
}
myDBContext.APMasterDBC.SubmitChanges();
TempData["flash"] = "Your Channel Has Been Created";
return RedirectToAction("Index");
}
else
{
TempData["flash"] = "Your Channel Has Not Been Created";
}
return View(channelModel);
}
正如你所看到的我正试图从MultiSelectList中插入许多广告插槽。但是我一直在收到:没有为此对象定义的无参数构造函数。
有谁知道这是为什么?有人可以告诉我我做错了什么吗?它为什么扔这个?