仅在第一个循环上需要MVC make list属性 [英] MVC make list property required on first loop only

查看:44
本文介绍了仅在第一个循环上需要MVC make list属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我将以下列表属性呈现到我的创建视图中:

  @for(var i = 0; i< Model.SiteSplits.Count(); i ++){< div class ="clearfix"></div>< div class ="form-group">@ Html.LabelFor(model => model.SiteSplits [i] .CostCode,htmlAttributes:new {@class ="control-label col-md-3"})< div class ="col-md-9">@ Html.EditorFor(model => model.SiteSplits [i] .CostCode,新的{htmlAttributes =新的{@class ="form-control"}})</div></div>< div class ="clearfix"></div>< div class ="form-group">@ Html.LabelFor(model => model.SiteSplits [i] .SplitPercentage,htmlAttributes:new {@class ="control-label col-md-3"})< div class ="col-md-9">@ Html.EditorFor(model => model.SiteSplits [i] .SplitPercentage,新{htmlAttributes =新{@class ="form-control"}})</div></div>} 

这将生成x3成本代码和拆分百分比,并在POST列表中传递回控制器.

我在视图模型的 CostCode SplitPercentage 上放置了必需的批注,这导致全部3个拆分/代码"都是必需的.

有没有办法仅在第一个循环上使 CostCode SplitPercentage 必需,是否忽略了对后两个的验证,因此它们只是可选的?

解决方案

要直接回答您的问题,您可以在模型中添加一个额外的属性,例如

  public bool IsRequired {get;放;} 

,然后在万无一失 [RequiredIfTrue] 或类似的验证属性上其他属性

  [RequiredIfTrue("IsRequired")]公共字符串CostCode {get;放;} 

并在GET方法中,使用3个默认对象填充 SiteSplits 集合,并使用 IsRequired = true; 设置第一个对象,并为包含隐藏的输入> IsRequired 属性.

  @ Html.HiddenFor(m => m.SiteSplits [i] .IsRequired) 

但是,对于您的最后一个问题您已指出要在1到3个项目之间的位置.如果用户添加了全部3个,则最后2个将不被验证,您可能会保存无效数据.

一个更好的选择是允许用户根据需要动态添加新项目,这可以通过使用 BeginCollectionItem 帮助器或通过创建html模板并将其复制并附加到您的集合中来完成(并更新其集合索引器),如以下答案中所述

  1. 发布未成功的表单数组
  2. 提交相同的部分视图多次数据控制器?
  3. 在一张桌子

此DotNetFiddle ,该示例基于基于您上一个问题中发布的模型的html模板,作为工作示例

Say I have the following list properties being rendered to my create view:

        @for (var i = 0; i < Model.SiteSplits.Count(); i++)
        {
            <div class="clearfix"></div>
            <div class="form-group">
                @Html.LabelFor(model => model.SiteSplits[i].CostCode, htmlAttributes: new {@class = "control-label col-md-3"})
                <div class="col-md-9">
                    @Html.EditorFor(model => model.SiteSplits[i].CostCode, new {htmlAttributes = new {@class = "form-control"}})
                </div>
            </div>

            <div class="clearfix"></div>
            <div class="form-group">
                @Html.LabelFor(model => model.SiteSplits[i].SplitPercentage, htmlAttributes: new {@class = "control-label col-md-3"})
                <div class="col-md-9">
                    @Html.EditorFor(model => model.SiteSplits[i].SplitPercentage, new {htmlAttributes = new {@class = "form-control"}})
                </div>
            </div>
        }

This results in x3 cost code and split percentages which are passed back to the controller in a list on POST.

I've put a required annotation on the CostCode and SplitPercentage in the view model, which is resulting in all 3 of the Split/Codes being required.

Is there any way of making just the CostCode and SplitPercentage Required on just the first loop and have it ignore the validation for the second two so they are merely optional?

解决方案

To directly answer your question, you could include an addition property in your model say

public bool IsRequired { get; set; }

and then use a foolproof [RequiredIfTrue] or similar validation attribute on the other properties

[RequiredIfTrue("IsRequired")]
public string CostCode { get; set; }

and in the GET method, populate the SiteSplits collection with 3 default objects, setting the first one with IsRequired = true; and include a hidden input for the IsRequired property.

@Html.HiddenFor(m => m.SiteSplits[i].IsRequired)

However, this makes no sense in relation to your last question where you have indicated you want between 1 and 3 items. If the user added all 3, then the last 2 would not be validated and you could be saving invalid data.

A better option is to allow the user to dynamically add new items as required, which can be done by using the BeginCollectionItem helper or by creating a html template and copying and appending it to your collection (and updating its collection indexers) as discussed in the following answers

  1. POST a form array without successful
  2. Submit same Partial View called multiple times data to controller?
  3. Set class validation for dynamic textbox in a table

An example of using the BeginCollectionItem is also included in this article

Refer also this DotNetFiddle for a working example using a html template based on the models posted in your last question

这篇关于仅在第一个循环上需要MVC make list属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆