使用 DropDownListFor 编辑日期时间成员变量不会保存
因此,我尝试创建一个 DropDownList
来编辑 DateTime
类具有的各种成员变量(我的模型中有一个实例),例如 Day,月份和年份。但是,当在 DropDownList
中选择某个项目并单击Save
输入按钮时,数据不会保存。所有其他已编辑的数据片段都将被更改并保存,但 DateTime
字段将不会更新。我不想只为我的约会对象制作一个新模型,但这是可以做到的。我可以创建 SelectList
,我在 HTML Helper
中执行此操作,如下所示:
namespace ErrorReport.Helpers
{
public class DateList
{
public static IEnumerable<SelectListItem> DayList
{
get
{
var days = new List<SelectListItem>();
for (int i = 1; i < 32; i++)
{
days.Add(new SelectListItem
{
Value = i.ToString(),
Text = i.ToString()
});
}
return days;
}
}
显然还创建了两个列表,一个用于年份,一个用于月份(我'我不关心小时、分钟或秒),没有显示它们,因为代码是相同的。在我的 View
中,我的编辑器代码如下所示,我要更改的变量是 CmpD
(属于 DateTime
类):
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"
type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"
type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Edit Report: "@Model.Title"</legend>
@Html.HiddenFor(model => model.ReportId)
@Html.HiddenFor(model => model.SbmD)
@Html.HiddenFor(model => model.UserName)
@Html.HiddenFor(model => model.CmpD)
...irrelevant editor code...
<div class="editor-label">
@Html.LabelFor(model => model.CmpD, "Estimated Completion Date:")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.CmpD.Month, DateList.MonthList)
@Html.DropDownListFor(model => model.CmpD.Day, DateList.DayList)
@Html.DropDownListFor(model => model.CmpD.Year, DateList.YearList)
//@Html.ValidationMessageFor(model => model.CmpD.Month)
//Above line was commented out
</div>
<p>
<input type="submit" value="Save"/>
</p>
</fieldset>
}
我做了之前对 CmpD 的成员变量进行了验证(仅是我想要更改的变量),将它们注释掉,因为它们不断为每个可能的 SelectList 选择抛出验证错误(在上面的代码)。我没有将参数放入 BeginForm
函数中,因为它们会导致保存其他数据时出现保存问题。我还为 CmpD
添加了 HiddenFor
字段(这是我的 DateTime
),以便正确保存所有内容,因为如果没有该行代码,则返回控制器未将模型识别为有效
并且未保存它。我尝试在我未使用的 DateTime
类中为每个成员变量添加一个 HiddenFor
字段,如果删除HiddenFor(model => model.CmpD)
,即使存在其他隐藏字段
。我也尝试过以这种方式制作列表:
public static IEnumerable<int> YearList
{
get
{
var years = new List<int>();
for (int i = 0; i < 4; i++)
{
years.Add(i);
}
return years.AsEnumerable();
}
}
但是,这会阻止 Html.DropDownListFor
函数完全工作,它显然必须使用 SelectList items
。我注意到只有strings
可以是SelectList items
,并且DateTime
成员变量是ints
。这是造成问题的原因吗?因为我看不到它们在哪里被重新转换为 ints
并且无法弄清楚我将如何做到这一点。基本上,如何编辑 DropDownList
中的 DateTime 成员变量
?
另外:内联代码是否过多?提前致谢!
So, I am trying to create a DropDownList
that will edit the various member variables the DateTime
class has (which I have an instance of in my model), such as Day, Month, and Year. However, when an item is selected in the DropDownList
and the Save
input button is clicked, the data does not save. All other edited pieces of data will be changed and saved, but the DateTime
field will just not update. I'd rather not make a new model just for my Dates, but it can be done. I can create the SelectList
, I do so in an HTML Helper
, shown below:
namespace ErrorReport.Helpers
{
public class DateList
{
public static IEnumerable<SelectListItem> DayList
{
get
{
var days = new List<SelectListItem>();
for (int i = 1; i < 32; i++)
{
days.Add(new SelectListItem
{
Value = i.ToString(),
Text = i.ToString()
});
}
return days;
}
}
There's obviously two more Lists that get made, one for Year and one for Month (I'm not bothering with hours, minutes, or seconds), didn't show them since the code is identical. In my View
, my editor code looks like the below, and the variable I want to change is CmpD
(of DateTime
class):
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"
type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"
type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Edit Report: "@Model.Title"</legend>
@Html.HiddenFor(model => model.ReportId)
@Html.HiddenFor(model => model.SbmD)
@Html.HiddenFor(model => model.UserName)
@Html.HiddenFor(model => model.CmpD)
...irrelevant editor code...
<div class="editor-label">
@Html.LabelFor(model => model.CmpD, "Estimated Completion Date:")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.CmpD.Month, DateList.MonthList)
@Html.DropDownListFor(model => model.CmpD.Day, DateList.DayList)
@Html.DropDownListFor(model => model.CmpD.Year, DateList.YearList)
//@Html.ValidationMessageFor(model => model.CmpD.Month)
//Above line was commented out
</div>
<p>
<input type="submit" value="Save"/>
</p>
</fieldset>
}
I did have validation in place for CmpD's
member variables earlier (only the ones I wanted to change), commented them out because they kept throwing validation errors for every possible SelectList
choice (marked in above code). I didn't put arguments in the BeginForm
function because they caused save problems with saving the other data. I also added the HiddenFor
field for CmpD
(which is my DateTime
) to get everything to save properly, since without that line of code the return controller did not recognize the Model as valid
and didn't save it. I tried adding a HiddenFor
field for every member variable in the DateTime
class I am not using, and I still get Validation errors if I remove the HiddenFor(model => model.CmpD)
, even with other Hidden Fields
present. I have also tried to make a list this way:
public static IEnumerable<int> YearList
{
get
{
var years = new List<int>();
for (int i = 0; i < 4; i++)
{
years.Add(i);
}
return years.AsEnumerable();
}
}
However, this prevents the Html.DropDownListFor
function from working at all, it apparently has to use SelectList items
. I have noticed that only strings
can be SelectList items
, and that the DateTime
member variables are ints
. Is that causing the problem? Because I don't see where they are recasted to ints
and cannot figure out how I would do that. Basically, how do I edit DateTime member variables
in a DropDownList
?
Also: Is this too much inline code? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DateTime 属性(日、月等)都是只读的。因此,不可能在 DateTime 实例上设置它们。
您需要在模型中设置可设置的 int 属性,以便能够更新下拉列表中的值。然后您可以稍后根据这些值构造一个 DateTime。我可能会为此目的创建一个单独的模型类,并将日期编辑器实现为单独的编辑器模板或处理所有细节的部分视图(以保持代码干净)。
The DateTime properties (Day, Month, etc.) are all read-only. Hence, it is not possible to set them on a DateTime instance.
You would need settable int properties in your model to be able to update the values from the drop-downs. Then you can construct a DateTime from those values later. I would probably create a separate model class just for this purpose and implement the date editor as separate editor template or partial view that handles all the details (to keep your code clean).