c#mvc html editorfor默认动态值

时间:2015-09-25 00:48:42

标签: c# asp.net-mvc-4

enter image description here这是我的控制器C#代码。

var timeList = new List<string>
{
    "8.00 AM",
    "8.15 AM",
    "8.30 AM",
    "8.45 AM",
    "9.00 AM",
    "9.15 AM",
    "9.30 AM"
    "17.00 PM"
};
ViewBag.TimeList = timeList;

是否有修改我的上述代码开始时间从8.00到17.00,间隔15分钟显示我的timeList?

这是我的View模型

@foreach (var T1 in ViewBag.TimeList)
{
    @T1
}

我已经添加了我的代码@Value = @T1,但它无效

@for (int i = 0; i < 3; i++)
{
    @Html.EditorFor(m => m.Time, new { htmlAttributes = new { @class = "form-control myPicker", @Value = @T1 } })
    <br />
    <span id="mySpan-@i"></span>
}      

任何人都可以告诉我如何在页面首次加载时自动填写网页表单时间间隔(例如,8.00,8.15..etc)。

1 个答案:

答案 0 :(得分:1)

目前还不清楚ViewBag.TimeList属性的用途是什么,但为了显示您的文本框,您需要设置模型的Time属性的值。

在GET方法中

model.Time = new List<string>(){ "8.00 AM", "8.15 AM", "8.30 AM" }); // set the initial default values
....
return View(model);

并在视图中

for(int i = 0; i < Model.Time.Count; i++)
{
  @Html.TextBoxFor(m => m.Time[i], new { @class = "form-control myPicker" })
}