Rob Rob - 1 month ago 13
C# Question

c# mvc html editorfor default dynamic value

enter image description hereThis is my controller C# code.

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;


Are there anyways to modify my above code start time from 8.00 to 17.00 with 15 mints interval with display my timeList?

This is my View model

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


I have already added my code
@Value = @T1
but it is not working

@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>
}


Can anyone please tell me how do I automatically fill out web forms time with interval (eg, 8.00, 8.15..etc) when page first loading.

Answer

Its unclear what the purpose of the ViewBag.TimeList property is, but in order to display your textboxes, you need to set the value of the Time property of your model.

In the GET method

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

and in the view

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