Html.DropDownListFor selected value 未设置为 selected

问题描述

我打印下拉菜单代码是这样的

        @Html.DropDownListFor(n => n.LevelTwo,new SelectList(""),"My selected value",new { @class = "custom-select" })

这给了我这样的输出

<select class="custom-select"   id="LevelTwo" name="LevelTwo">
<option value="">My selected value</option>
但我希望它是这样的
    <select class="custom-select"   id="LevelTwo" name="LevelTwo">
<option selected="selected" value="My selected value">My selected value</option>

我怎样才能做到这一点? 注意:下拉列表的其他元素将在稍后使用 Ajax 添加。这不会破坏上面解释的用例

解决方法

将您的代码更改为:

class Revision extends StatefulWidget {
  Revision({Key key,this.title}) : super(key: key);
  String title;
  @override
  _RevisionState createState() => _RevisionState();
}

class _RevisionState extends State<Revision> with TickerProviderStateMixin {
  //debut code events
  CalendarController _controller;
  Map<DateTime,List> _events;
  Map<DateTime,List> _selectedEvents;
  DateTime selectedDay = DateTime.now();
  DateTime focusedDay = DateTime.now();
  TextEditingController dateController;
  SharedPreferences prefs;
  int status;
  bool _autovalidate = false;
  Datum datum = Datum();

 @override
  void initState() {
    super.initState();
    final _selectedDay = DateTime.now();
    _selectedEvents = {};
    _controller = CalendarController();
    _events = Map<DateTime,List>();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      getTask1().then((val) => setState(() {
            _events = val;
          }));
      print(' ${_events.toString()} ');
    });

    initializeDateFormatting();
    prefsData();
  }

  Widget _buildTableCalendarWithBuilders() {
    return TableCalendar(
      //events calendar
      events: _events,daysOfWeekStyle: DaysOfWeekStyle(
          weekdayStyle: TextStyle(color: Colors.white),weekendStyle: TextStyle(color: KYellow)),calendarController: _controller,locale: 'fr_FR',initialCalendarFormat: CalendarFormat.week,//Calendar style
      calendarStyle: CalendarStyle(
          markersColor: Colors.white70,canEventMarkersOverflow: true,todayColor: KYellow,selectedColor: KYellow,todayStyle: TextStyle(
              fontWeight: FontWeight.bold,fontSize: 22.0,color: Colors.white)),//header style
      headerStyle: HeaderStyle(
        leftChevronIcon: Icon(
          CommunityMaterialIcons.chevron_left,color: Colors.white,),rightChevronIcon: Icon(
          CommunityMaterialIcons.chevron_right,centerHeaderTitle: true,titleTextStyle: TextStyle(color: Colors.white),formatButtonDecoration: BoxDecoration(
          border: Border.all(color: KYellow),color: Colors.transparent,borderRadius: BorderRadius.circular(22.0),formatButtonTextStyle: TextStyle(color: KYellow),formatButtonShowsNext: false,startingDayOfWeek: StartingDayOfWeek.sunday,//ondaySelected
      onDaySelected: (date,events,holidays) {
        _onDaySelected(date,events);
        /*  setState(() {
          _selectedEvents = events as Map<DateTime,List>;
          print(_selectedEvents);
        });*/
      },//calandar builder
      builders: CalendarBuilders(
        dayBuilder: (context,date,events) => Container(
          margin: const EdgeInsets.all(12.0),alignment: Alignment.center,child: Text(
            date.day.toString(),style: TextStyle(color: Colors.white),selectedDayBuilder: (context,events) => Container(
            margin: const EdgeInsets.all(12.0),decoration: BoxDecoration(
                color: KYellow,borderRadius: BorderRadius.circular(30)),child: Text(
              date.day.toString(),)),todayDayBuilder: (context,decoration: BoxDecoration(
                color: KYellow.withOpacity(0.3),);
  }


Widget _buildEventList() {
    return Column(children: [
      ..._selectedEvents().map(
        (event) => Padding(
          padding: const EdgeInsets.all(8.0),child: Card(
              child: Padding(
                  padding: EdgeInsets.all(20),child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,children: [
                          Icon(
                            CommunityMaterialIcons.oil,color: KYellow,SizedBox(
                            width: 10,Text(
                            event.revisionTitle,style: TextStyle(
                                color: KYellow,fontSize: 16,fontWeight: FontWeight.w500),)
                        ],SizedBox(
                        height: 20,Text(event.revisionTitle,style: TextStyle(
                              color: KYellow,fontSize: 12,fontWeight: FontWeight.w500)),Text(
                        'Dernier visite effectuée le '
                        ' ${event.revisionDate}',style: TextStyle(color: Colors.indigo[400]),SizedBox(
                        height: 10,Row(
                        mainAxisAlignment: MainAxisAlignment.end,children: [
                          IconButton(
                            icon: Icon(
                              CommunityMaterialIcons.file_clock,size: 35,onPressed: () {},SizedBox(
                            width: 30,IconButton(
                            icon: Icon(
                              CommunityMaterialIcons.content_save_edit_outline,onPressed: () {
                              if (event == 'Vidange') {
                                showDialog(
                                  context: context,builder: (BuildContext context) =>
                                      _buildUpdateVidengeEvent(context),);
                              } else
                                showDialog(
                                  context: context,builder: (BuildContext context) =>
                                      _buildUpdateEvent(context),);
                            },)
                    ],))),)
    ]);
  }

Future<Map<DateTime,List>> getTask1() async {
    Map<DateTime,List> mapFetch = {};
    List<Datum> event = await revisionApi.getAllRevision();
    for (int i = 0; i < event.length; i++) {
      var createTime = DateTime.parse(
        event[i].revisionDate,);

      var original = mapFetch[createTime];
      print(original);
      if (original == null) {
        print("null");
        mapFetch[createTime] = [event[i]];
      } else {
        print(event[i].revisionLocation);
        mapFetch[createTime] = List.from(original)..addAll([event[i]]);
      }
    }

   return mapFetch;
  }
}
  

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...