带有一个或多个操作的单行if语句

时间:2017-11-19 09:42:50

标签: c# asp.net-mvc if-statement razor

我想做一行多行动的if行。

默认为:

 @if (Reservation.Status == RSVStatusType.Cancelled)
                            {
                                <div class="row no-gutters alert-danger">
                                    <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                                        @Resource.Reservation_Status
                                    </div>
                                    <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
                                        @Reservation.Status
                                    </div>                                           
                                </div>
                            }
                           else if (Reservation.Status == RSVStatusType.Booked)
                            {
                                <div class="row no-gutters alert-info">
                                    <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                                        @Resource.Reservation_Status
                                    </div>
                                    <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
                                        @Reservation.Status
                                    </div>                                    
                                </div>
                            }

我还需要根据动作或条件传递alert-danger或alert-info css类。

我需要这样的东西

   @(reservation.status == cancelled ? "danger" : "info")

我尝试过的任何建议都不止于此,但我确实得到了解决方案。

1 个答案:

答案 0 :(得分:0)

这是答案

 @{var className = Reservation.Status == RSVStatusType.Cancelled ? "row no-gutters alert-danger" : "row no-gutters alert-info";}
                                <div class="@className">
                                    <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                                        @Resource.Reservation_Status
                                    </div>
                                    <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
                                        @Reservation.Status
                                    </div>
                                </div>