-1

This is my edit page. I want the default value to be the time received from the database called @l.begginingTime, which will the same as one of the values in the dropdown.

How do I do this?

@foreach (var l in Model.Cleaner.TimeAvailables)
  {     
    <select class="form-control dropDown"  name="Sunday1From" value="@l.BegginingTime">
        <option value="">Unavailable</option>
        <option value="00:00:00">12:00 AM</option>
        <option value="00:30:00">12:30 AM</option>
        <option value="01:00:00">1:00 AM</option>
        <option value="01:30:00">1:30 AM</option>
        <option value="02:00:00">2:00 AM</option>
        <option value="02:30:00">2:30 AM</option>
    </select>
</td>
Newbie
  • 239
  • 2
  • 11

4 Answers4

1

You should be able to use the attribute selected. Add that to whichever option in the dropdown you want to be selected by default.

Reference this post for a similar question.

Community
  • 1
  • 1
colinharris05
  • 94
  • 2
  • 12
  • But it's not always the same? For each user it is a different time? – Newbie Dec 07 '16 at 21:33
  • Sure, well it doesn't look like your options are dynamic right now. If you wanted a dynamic option to hold the value of the variable then try something like this – colinharris05 Dec 07 '16 at 21:35
  • @l.BegginingTime – colinharris05 Dec 07 '16 at 21:37
  • hmm this seems like the easiest. But I need to display it as Am/PM But when I try to change it to Datetime in order to display it as such I am getting an error. Any idea why this wouldn't work? @string.Format("{0:hh:mm:ss tt}", new DateTime().Add(l.BegginingTime)) Error: The best overloaded method match for 'System.DateTime.Add(System.TimeSpan)' has some invalid arguments – Newbie Dec 07 '16 at 21:56
1

Add your dynamic value as an option then set the selected attribute

@foreach (var l in Model.Cleaner.TimeAvailables)
      {     
        <select class="form-control dropDown"  name="Sunday1From">
            <option value="@l.BegginingTime" selected>@l.BegginingTime</option>
            <option value="00:00:00">12:00 AM</option>
            <option value="00:30:00">12:30 AM</option>
            <option value="01:00:00">1:00 AM</option>
            <option value="01:30:00">1:30 AM</option>
            <option value="02:00:00">2:00 AM</option>
            <option value="02:30:00">2:30 AM</option>
        </select>
Stinky Towel
  • 768
  • 6
  • 26
0

You can also use jQuery and javascript

$(document).ready(function() {
    $('.dropDown').val(beginTime);
}
Yaman
  • 1,030
  • 17
  • 33
0

You are using a html select which is completely separate from your model. So there is two option here.

Create your dropdown with MVC like this:

@Html.DropDownList("Model.Cleaner.TimeAvailables", (SelectList)@l.BegginingTime) 
Yaser
  • 5,609
  • 1
  • 15
  • 27