Asp.net mvc MVC视图。字符串列表。对列表项有条件地使用DateTime EditorTemplate
该视图是一个“申请表” 该模型是一个“应用程序”对象 应用程序对象包含一个Person对象和一个Question对象数组 每个问题都有:Asp.net mvc MVC视图。字符串列表。对列表项有条件地使用DateTime EditorTemplate,asp.net-mvc,razor,Asp.net Mvc,Razor,该视图是一个“申请表” 该模型是一个“应用程序”对象 应用程序对象包含一个Person对象和一个Question对象数组 每个问题都有: 包含问题的文本字段 包含问题类型的数字字段(1=数字,2=字符串,3=日期等) 响应的文本字段。响应字段预先加载了一个示例(对于日期,它是DateTime.Now.ToShortDateString()) 该视图有一个包含人员详细信息的表单和一个包含问题列表的表。申请人填写响应字段并提交。这很有效 现在我想用DatetimePicker演示日期类型问题 使
- 包含问题的文本字段
- 包含问题类型的数字字段(1=数字,2=字符串,3=日期等)
- 响应的文本字段。响应字段预先加载了一个示例(对于日期,它是DateTime.Now.ToShortDateString())
<td>
<div class="col-md-10">
@{
if (Model.Responses[i].QuestionTypeId == 3)
{
@Html.EditorFor(p => p.Responses[i].Response,"DateTime")
@Html.ValidationMessageFor(p => p.Responses[i].Response)
}
else
{
@Html.TextBoxFor(p => p.Responses[i].Response)
@Html.ValidationMessageFor(p => p.Responses[i].Response)
}
}
@*@Html.TextBoxFor(p => p.Responses[i].Response)
@Html.ValidationMessageFor(p => p.Responses[i].Response)*@
</div>
</td>
@{
if(Model.Responses[i].QuestionTypeId==3)
{
@EditorFor(p=>p.Responses[i].响应,“DateTime”)
@Html.ValidationMessageFor(p=>p.Responses[i].Response)
}
其他的
{
@Html.TextBoxFor(p=>p.Responses[i].Response)
@Html.ValidationMessageFor(p=>p.Responses[i].Response)
}
}
@*@Html.TextBoxFor(p=>p.Responses[i].Response)
@Html.ValidationMessageFor(p=>p.Responses[i].Response)*@
为了运行DateTime.cshtml EditorTemplate,我必须将模型类型设置为字符串
尽管它被调用,但它不执行datepicker javascript
我假设这是因为底层模型字段类型是字符串
我尝试用模板中的字符串创建一个日期,并将日期传递到模板文本框调用中,但没有效果
有没有办法克服这一点
EditorTemplate代码当前为:
@model System.String
@{ DateTime myDate = DateTime.Parse(Model);}
@Html.TextBox("", myDate,new { @class = "datePicker" ,
})
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/cssjqryUi")
<script type="text/javascript">
$(function () {
$(".datePicker").datepicker({
showOn: "button",
buttonImage: "/images/calendar-icon.png",
buttonImageOnly: true,
buttonText: "Select date"
});
});
</script>
}
@model System.String
@{DateTime myDate=DateTime.Parse(Model);}
@TextBox(“,myDate,new{@class=“datePicker”,
})
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
@Scripts.Render(“~/bundles/jqueryui”)
@style.Render(“~/Content/cssjqryUi”)
$(函数(){
$(“.datePicker”).datePicker({
展示:“按钮”,
buttonImage:“/images/calendar icon.png”,
buttonImageOnly:正确,
按钮文字:“选择日期”
});
});
}
为什么需要将其设置为字符串
?而且脚本永远不应该在模板(或部分)中——只应该在布局的主视图中。只需使用TextBoxFor()
并给它一个类名,然后在主视图中使用$(“.datePicker”).datePicker({….
数据来源于一个系统,其中问题的回答是字符串。这是一个通用的“应用程序”系统,用户在该系统中决定应用程序应采用何种形式。设想有人为新应用程序制作铅笔表单,复制照片并分发。问题将由“应用程序”需求决定。H问题.Response字段是一个字符串。它可以是“数字”或“日期”或“真/假”或金额$0.00。引用必须返回到模型,以便提交的对象完好无损。即显示的问题数组。提交的问题数组。事实上,您的问题使我重新考虑了设计。没有理由更改日期“不能从列表中移出,因为它们将是每个应用程序的一项功能。这将解决问题。它们可以在“范围对象”中显示为日期类型。如果您将您的评论框定为答案,我会将其标记为已接受。谢谢。”