【问题标题】:Check if property from Model in Asp.Net Mvc is not null in javascript function检查Asp.Net Mvc中模型的属性在javascript函数中是否不为空
【发布时间】:2017-08-10 09:17:37
【问题描述】:

我在 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”时不显示部分视图 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>

【问题讨论】:

  • 您可以简化您的条件,只需使用if (a) { 。在这种情况下,所有虚假值都会将代码重定向到 else 分支
  • 如果这条指令 var a = $("#SelectedHouseDetailsText").value 是正确的,我采纳了你的建议,但条件“a”为真
  • 我认为这个$("#SelectedHouseDetailsText").value 无效,它必须是$("#SelectedHouseDetailsText").val()

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


【解决方案1】:

您可以在 javascript 中检查它,如下所示。实际上“@Html.Partial”是服务器端调用。所以它会在 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>

【讨论】:

  • 当我打开这个视图时 '$('.divForSelectHouse').Load('@Html.Partial("~/Views/ClientReservations/ReservationHouseDetails.cshtml",Model.MyProperty[Model.SelectedHouseDetailsText] )')' 始终执行此行 值不能为空。参数名称:key
  • 我已经用未定义的检查编辑了我的答案。你试过了吗?
  • 问题依然存在,如果这个脚本应该包含在 View.cshtml 的 head 部分?,版本 jquery 是 3.1.1
  • 我找到了!!!实际上“@Html.Partial”是服务器端调用。所以它会在 JS 之前被调用。这就是问题所在。所以你应该有服务器端条件来解决这个问题。我更新了你能看到吗??
  • 您可以根据业务需要在任何地方添加该条件:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
  • 2020-06-29
  • 1970-01-01
  • 2013-01-12
相关资源
最近更新 更多