0

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.

12
  • Unclear what your wanting to do. @Value = @T1 cannot work since T1 does not exist in the context of the loop (and in any case you should NEVER attempt to set the value attribute when using a HtmlHelper). What is the reason to the @for (int i = 0; i < 3; i++) loop? Commented Sep 25, 2015 at 0:59
  • I am sorry, I have already removed @for (int i = 0; i < 3; i++) loop. It is not necessary. Commented Sep 25, 2015 at 1:06
  • You still have a @for (int i = 0; i < 3; i++) loop for creating 3 identical inputs. Your code does not make any sense so its a bit hard to understand what you want the output of that loop to be. And what is the type of property Time? Commented Sep 25, 2015 at 1:07
  • I am using model IList, my C# code var timeList = new List<string> time string and Time property is string because I need only time valve. Commented Sep 25, 2015 at 1:14
  • So are you wanting to display a list box to select times? And what is the for loop for? You need to explain what you want the output to be (by editing your question). Commented Sep 25, 2015 at 1:15

1 Answer 1

1

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" })
}
Sign up to request clarification or add additional context in comments.

3 Comments

I used ViewBag.TimeList because I have GET method return another value. Are there anyway to add multiple return value example return View(DataList, model);
Not clear what your asking. You need to ask a new question which the relevant code - I have already answered this one :)
Pleasure, but your already have one downvote and I almost did the same. Next time you ask a question, explain (1) what your trying to achieve (2) the relevant code (which in this case should have included what the Time property is since you use it in the view) and (3) what is actually happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.