1
我對MVC還很陌生,所以請耐心等待。MVC3驗證局部視圖
我有一個包含局部視圖的簡單視圖。在這種局部視圖模型我有驗證的註釋,像這樣:
public class LocationViewModel
{
[Display(Name = "Area")]
[UIHint("DropDownList")]
public int? AreaId { get; set; }
public string Area { get; set; }
public IEnumerable<AWS.DTO.Lookup> Areas { get; set; }
[Display(Name = "Establishment")]
[UIHint("DropDownList")]
public int? EstablishmentId { get; set; }
public string Establishment { get; set; }
public IEnumerable<AWS.DTO.Lookup> Establishments { get; set; }
[Display(Name = "Property")]
[UIHint("DropDownList")]
[Required(ErrorMessage = "Category is required.")]
public int PropertyId { get; set; }
public string Property { get; set; }
public IEnumerable<AWS.DTO.Lookup> Properties { get; set; }
}
局部視圖:
@model AWS.PL.ViewModels.LocationViewModel
<script type="text/javascript">
function getSEATs(area) {
$.ajax({
url: "@Url.Action("SelectSEATs", "Location")",
data: { areaId: area },
dataType: "json",
type: "POST",
error: function() {
alert("An error occurred.");
},
success: function (data) {
var items = "";
$.each(data, function (i, item) {
items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
});
$("#SEATId").html(items);
}
});
}
function getEstablishments(seat) {
$.ajax({
url: "@Url.Action("SelectEstablishments", "Location")",
data: { seatId: seat },
dataType: "json",
type: "POST",
error: function() {
alert("An error occurred.");
},
success: function (data) {
var items = "";
$.each(data, function (i, item) {
items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
});
$("#EstablishmentId").html(items);
}
});
}
function getProperties(estab) {
$.ajax({
url: "@Url.Action("SelectProperties", "Location")",
data: { estabId: estab },
dataType: "json",
type: "POST",
error: function() {
alert("An error occurred.");
},
success: function (data) {
var items = "";
$.each(data, function (i, item) {
items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
});
$("#PropertyId").html(items);
}
});
}
$(document).ready(function() {
$("#AreaId").change(function() {
var area = $("#AreaId").val();
getSEATs(area);
});
$("#SEATId").change(function() {
var seat = $("#SEATId").val();
getEstablishments(seat);
});
$("#EstablishmentId").change(function() {
var estab = $("#EstablishmentId").val();
getProperties(estab);
});
});
</script>
<div class="control-group">
@Html.LabelFor(m => m.AreaId, new { @class = "control-label" })
<div class="controls">
<div>
@Html.DropDownListFor(m => m.AreaId, new SelectList(Model.Areas, "ID", "Description", -1), "-- Please Select -- ")
</div>
</div>
</div>
<div class="control-group">
@Html.LabelFor(m => m.SEATId, new { @class = "control-label" })
<div class="controls">
<div>
@Html.DropDownListFor(m => m.SEATId, new SelectList(Model.SEATs, "ID", "Description", -1))
</div>
</div>
</div>
<div class="control-group">
@Html.LabelFor(m => m.EstablishmentId, new { @class = "control-label" })
<div class="controls">
<div>
@Html.DropDownListFor(m => m.EstablishmentId, new SelectList(Model.Establishments, "ID", "Description", -1))
</div>
</div>
</div>
<div class="control-group">
@Html.LabelFor(m => m.PropertyId, new { @class = "control-label" })
<div class="controls">
<div>
@Html.DropDownListFor(m => m.PropertyId, new SelectList(Model.Properties, "ID", "Description", -1))
@Html.ValidationMessageFor(m => m.PropertyId)
</div>
</div>
</div>
當我提交我的主要形式驗證的場發射了主視圖,而不是局部的視圖。誰能告訴我爲什麼?
謝謝, 保羅。
你的局部視圖查找是什麼樣的?在局部視圖中使用模型的哪些屬性? –
我已經添加了部分視圖代碼。主模型使用propertyid屬性。 – Wilky
啊,這樣驗證註釋需要駐留在主屬性上而不是部分視圖模型屬性? – Wilky