C# DateTime Selectlist从模型到视图再到控制器
我正在努力用MVC和EF制作我的第一个游戏 我有一门课叫赛跑。 这有一个DateTime字段,用于保存服务器将自动执行比赛的时间 现在,当用户创建此比赛时。他们必须在这里选择一个约会时间。 列表中显示的日期时间将添加到竞赛模型中。 我有一个时间列表,可以根据今天和接下来的2天转换为日期 我遇到的问题是。 使用干净的方法。我的选择列表为空 使用脏方法,我的模型无法保存,因为DateTime字段是字符串而不是DateTime字段 /*种族等级*/C# DateTime Selectlist从模型到视图再到控制器,c#,asp.net-mvc,asp.net-mvc-3,entity-framework-4,code-first,C#,Asp.net Mvc,Asp.net Mvc 3,Entity Framework 4,Code First,我正在努力用MVC和EF制作我的第一个游戏 我有一门课叫赛跑。 这有一个DateTime字段,用于保存服务器将自动执行比赛的时间 现在,当用户创建此比赛时。他们必须在这里选择一个约会时间。 列表中显示的日期时间将添加到竞赛模型中。 我有一个时间列表,可以根据今天和接下来的2天转换为日期 我遇到的问题是。 使用干净的方法。我的选择列表为空 使用脏方法,我的模型无法保存,因为DateTime字段是字符串而不是DateTime字段 /*种族等级*/ using System; using System
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace DOCCL.Models
{
public class Race
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
[Required(ErrorMessage = "Purse is required")]
public int Purse { get; set; }
public int? Slots { get; set; }
public int SlotPrice { get; set; }
public DateTime? RaceTime { get; set; }
public int? TrackId { get; set; }
public virtual Track Track { get; set; }
public int? OwnerId { get; set; }
public virtual User Owner { get; set; }
public virtual ICollection<Horse> RacingHorses { get; set; }
public virtual ICollection<RaceResult> RaceResults { get; set; }
public Race()
{
SlotPrice = 0; //default value
Slots = 8;
}
public SelectList SlotOptions
{
get
{
return new SelectList(AllSlotOptions, "Index", "Slot", Slots);
}
}
public static IEnumerable<SlotOption> AllSlotOptions
{
get
{
int[] slotOptions = { 8, 12, 16 };
int index = 1;
foreach (int slotOption in slotOptions)
{
yield return new SlotOption
{
Index = index,
Slot = slotOption,
};
++index;
}
}
}
public class SlotOption
{
public int Index { get; set; }
public int Slot { get; set; }
}
public SelectList TimeSlotOptions
{
get
{
return new SelectList(AllTimeSlotOptions, "Index", "Slot", RaceTime);
}
}
public static IEnumerable<TimeSlotOption> AllTimeSlotOptions
{
get
{
int[] slotOptions = { 9, 12, 15, 18, 21 };
int index = 1;
for (int day = 0; day <= 2; day++)
{
foreach (int slotOption in slotOptions)
{
DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + day, slotOption, 0, 0);
if (dt > DateTime.Now)
{
yield return new TimeSlotOption
{
Index = index,
Slot = dt
};
}
++index;
}
}
}
}
public class TimeSlotOption
{
public int Index { get; set; }
public DateTime Slot { get; set; }
}
}
}
/*竞赛\创造*/
@model DOCCL.Models.Race
@{
Layout = "~/Views/Shared/_frontend.cshtml";
ViewBag.Title = "Create";
}
<div class="ten columns alpha omega">
<h2>Create race</h2>
</div>
@using(Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend></legend>
<div class="ten columns alpha omega">
<div class="three columns alpha">@Html.LabelFor(race => race.Name)</div>
<div class="three columns">@Html.TextBoxFor(race => race.Name)</div>
<div class="four columns omega">@Html.ValidationMessageFor(race => race.Name)</div>
</div>
<div class="ten columns alpha omega">
<div class="three columns alpha">@Html.LabelFor(race => race.RaceTime)</div>
<div class="three columns">@Html.DropDownList("RaceTime", DOCCL.Models.Race.AllTimeSlotOptions.Select(ts => new SelectListItem {Value = ts.Slot.ToString(), Text = ts.Slot.ToString() }) , "-- Race --")</div>
<div class="four columns omega">@Html.ValidationMessageFor(race => race.RaceTime)</div>
</div>
<div class="ten columns alpha omega">
<div class="three columns alpha">@Html.LabelFor(race => race.Slots)</div>
<div class="three columns">@Html.DropDownList("Slots", DOCCL.Models.Race.AllSlotOptions.Select(ts => new SelectListItem {Value = ts.Index.ToString(), Text = ts.Slot.ToString() }) , "-- Slots --")</div>
<div class="four columns omega">@Html.ValidationMessageFor(race => race.Slots)</div>
</div>
<div class="ten columns alpha omega">
<div class="three columns alpha">@Html.LabelFor(race => race.SlotPrice)</div>
<div class="three columns">@Html.TextBoxFor(race => race.SlotPrice)</div>
<div class="four columns omega">@Html.ValidationMessageFor(race => race.SlotPrice)</div>
</div>
<div class="ten columns alpha omega">
<div class="offset-by-eight two columns alpha omega">
<input type="submit" class="button" value="Create" />
</div>
</div>
</fieldset>
}
/*干净的尝试*/
<div class="ten columns alpha omega">
<div class="three columns alpha">@Html.LabelFor(race => race.RaceTime)</div>
<div class="three columns">@Html.DropDownListFor(model => model.RaceTime, Model.TimeSlotOptions, "-- Race time --")</div>
<div class="four columns omega">@Html.ValidationMessageFor(race => race.RaceTime)</div>
</div>
<div class="ten columns alpha omega">
<div class="three columns alpha">@Html.LabelFor(race => race.Slots)</div>
<div class="three columns">@Html.DropDownListFor(model => model.Slots, Model.SlotOptions, "-- Slot --")</div>
<div class="four columns omega">@Html.ValidationMessageFor(race => race.Slots)</div>
</div>
显然,最后一个是最好的,它返回null,因为我实际上还没有Race对象。因此我无法访问selectlist
你们中有谁知道如何正确地做这件事吗?我想试试这样的方法
@Html.DropDownListFor(x => x.RaceTime, new SelectList(Model.TimeSlotOptions, "Id", "Name"), "-- Race time --")
而不是
@Html.DropDownListFor(model => model.RaceTime, Model.TimeSlotOptions, "-- Race time --")
我最终将这两个列表移动到一个公共帮助器类中。 并从那里实例化它们
成功了。仍在寻找对此的一些见解。如果有人有办法做到这一点,我非常乐意在必要时重新考虑。希望在制作这个游戏的过程中学习。尝试一下。同样的问题。空引用异常。
@Html.DropDownListFor(model => model.RaceTime, Model.TimeSlotOptions, "-- Race time --")