检查来自Asp.Net Mvc中Model的属性在javascript函数中是否为空

时间:2017-08-10 09:17:38

标签: javascript asp.net asp.net-mvc razor

我在Razor View.cshtml中有以下javascript函数

<script type="text/javascript">
        $(function () {
            $("#form").change(function () {
                var selected = $(this).find("option:selected").text();
                $("#SelectedHouseDetailsText").val(selected);
                var a = $("#SelectedHouseDetailsText").value;
                if ( a!= null) {
                    $('.divForSelectHouse').Load('@Html.Partial("~/Views/ClientReservations/ReservationHouseDetails.cshtml",Model.MyProperty[Model.SelectedHouseDetailsText])')
                }               
            });
        })
    </script>

当我打开此视图字段时“SelectedHouseDetailsText”未在初始化时,如何在键值“SelectedHouseDetailsText” “SelectedHouseDetailsText”时检查是否为空以不显示部分视图strong>未初始化?

模型的C#代码

public class ReservationDetails
    {
        public ReservationDetails()
        {

        }
        public Reservation Reservation { get; set; }

        public List<ReservationHouseDetails> ReservationHouseDetails { get; set; }

        public Dictionary<string,ReservationHouseDetails> MyProperty { get; set; }

        public List<ReservationAttractionDetails> ReservationAttractionDetails { get; set; }

        public IEnumerable<SelectListItem> Houses { get; set; }

        public int SelectedHouseDetailsId { get; set; }

        public string SelectedHouseDetailsText { get; set; }
    }

部分视图

@model Repository.ViewModels.ReservationHouseDetails
@using Repository.Models


<div>
    <h4>Szczegóły domku + @Html.DisplayFor(model=>model.House.Name)</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.House.HouseType.Type)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.House.HouseType.Type)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.House.HouseType.Price)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.House.HouseType.Price)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.House.Description)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.House.Description)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Meal.Type)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Meal.Type)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Meal.Price)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Meal.Price)
        </dd>
    </dl>

    <table class="table">
        <tr>
            <th>
                @Html.DisplayName("LP")
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Participants.FirstOrDefault().Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Participants.FirstOrDefault().Surname)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Participants.FirstOrDefault().BirthDate)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model.Participants.Select((value, i) => new { i, value }))
        {
            Participant value = item.value;
            int index = item.i + 1;
            <tr>
                <td>
                    @index.ToString()
                </td>
                <td>
                    @value.Name
                </td>
                <td>
                    @value.Surname
                </td>
                <td>
                    @value.BirthDate
                </td>
            </tr>
         }

</table>
</div>

1 个答案:

答案 0 :(得分:0)

你可以在下面的javascript中查看它。实际上&#34; @ Html.Partial&#34;是服务器端呼叫。所以它会在JS之前被调用。这就是问题所在。所以你应该有服务器端条件来解决这个问题。

<script type="text/javascript">
@if(!string.IsNullOrEmpty(Model.SelectedHouseDetailsText))
{
        <text>
           $(function () {
            $("#form").change(function () {
                var selected = $(this).find("option:selected").text();
                $("#SelectedHouseDetailsText").val(selected);
                var a = $("#SelectedHouseDetailsText").value;
                if (a) {
                    $('.divForSelectHouse').Load('@Html.Partial("~/Views/ClientReservations/ReservationHouseDetails.cshtml",Model.MyProperty[Model.SelectedHouseDetailsText])')
                }               
            });
        })
       </text>
}
    </script>