【问题标题】:MVC3 validating a partial viewMVC3 验证局部视图
【发布时间】:2013-07-06 00:55:42
【问题描述】:

我对 MVC 还是很陌生,所以请耐心等待。

我有一个包含局部视图的简单视图。在那个部分视图模型中,我有这样的验证注释:

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 属性。
  • 啊,所以验证注释需要驻留在主属性而不是部分视图模型属性上??
  • 提交时生成的html中下拉菜单的值是多少? required 属性检查字段是否不为空或不为空。由于您的所有 Select 输入都将是 int 类型,我认为您可能需要将 PropertyId 字段类型更改为 int?,并将下拉列表的默认值更改为 null。
  • 完全正确,它现在可以工作了。我可以不将此评论标记为答案吗?

标签: jquery asp.net-mvc asp.net-mvc-3


【解决方案1】:

由于属性字段是不可为空的 int,因此该字段将始终设置为某个值。 required 属性只检查空或空字段,而不是默认值。您需要将属性的类型更改为 int?,并将默认值设置为 null。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多