【发布时间】: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