gpt4 book ai didi

c# - Html.ListBoxFor() 的 ASP.NET MVC 问题

转载 作者:行者123 更新时间:2023-11-30 21:19:56 31 4
gpt4 key购买 nike

我在使用帮助程序 Html.ListBoxFor() 时遇到问题。

这是我的 View 模型:

public class NewReservation
{
public SelectList AvailableServiceDates { get; set; }
public DateTime SelectedServiceDate { get; set; }
}

这是我的操作方法:

    public virtual ActionResult New()
{
NewReservation newReservation = new NewReservation();
newReservation.AvailableServiceDates = new SelectList(nhSession.Linq<Service>().Select(x => x.DateTime).Distinct());
return View(newReservation);
}

在我看来:

<%:Html.ListBoxFor(x => x.SelectedServiceDate, Model.AvailableServiceDates) %>

异常(exception)情况:

Value cannot be null. Parameter name: source

Stack Trace:

[ArgumentNullException: Value cannot be null. Parameter name: source]
System.Linq.Enumerable.Cast(IEnumerable source) +4177011
System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, String optionLabel, String name, IEnumerable1 selectList,
Boolean allowMultiple, IDictionary
2 htmlAttributes) +723
System.Web.Mvc.Html.SelectExtensions.ListBoxHelper(HtmlHelper htmlHelper, String name, IEnumerable1
selectList, IDictionary
2 htmlAttributes) +47
System.Web.Mvc.Html.SelectExtensions.ListBoxFor(HtmlHelper1
htmlHelper, Expression
1 expression, IEnumerable1 selectList,
IDictionary
2 htmlAttributes) +113
System.Web.Mvc.Html.SelectExtensions.ListBoxFor(HtmlHelper1
htmlHelper, Expression
1 expression, IEnumerable`1 selectList) +85
ASP.views_reservation_new_aspx.__RenderMainContent(HtmlTextWriter __w, Control parameterContainer) in c:\code\GTCC Culinary Reservation\src\CulinaryReservation.WebMVC\Views\Reservation\New.aspx:11 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\code\GTCC Culinary Reservation\src\CulinaryReservation.WebMVC\Views\Shared\Site.Master:66 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +56
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060

This post描述了同样的问题。就像那篇文章一样,如果我将我的 NewReservation.SelectedServiceDate 从 DateTime 更改为字符串,一切似乎都有效。但我所读到的一切都表明这是一个应该在 MVC2 候选版本中修复的错误。

我做错了什么吗?我不希望属性的类型是字符串,它需要是 DateTime。我不应该能够使用反射(reflect)我的问题的对象吗?

最佳答案

多选列表可以为空(例如,如果用户未进行选择),这意味着您无法绑定(bind)到值类型(如 DateTime)。您可以改用可为 null 的 DateTime:

public class NewReservation
{
public SelectList AvailableServiceDates { get; set; }
public DateTime? SelectedServiceDate { get; set; }
}

另请注意,在多选列表中,用户可以执行多项选择,这意味着绑定(bind)到单个日期看起来不太自然。恕我直言,拥有这个会更自然:

public class NewReservation
{
public SelectList AvailableServiceDates { get; set; }
public IEnumerable<DateTime> SelectedServiceDates { get; set; }
}

关于c# - Html.ListBoxFor() 的 ASP.NET MVC 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3577946/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com