C# 列表中的模型绑定

C# 列表中的模型绑定,c#,asp.net-mvc,binding,C#,Asp.net Mvc,Binding,我在ViewModel中有一个列表,但是当我向控制器发送帖子时,它没有绑定它,并且在参数:null,值:null上显示了一个错误 如果你能帮我解决这个问题 视图模型: public class OperaRateImportRuleViewModel { public SelectList ResortsSelectList { get; set; } [Required] [DisplayName("Resorts")] public List<strin

我在ViewModel中有一个列表,但是当我向控制器发送帖子时,它没有绑定它,并且在参数:null,值:null上显示了一个错误

如果你能帮我解决这个问题

视图模型:

public class OperaRateImportRuleViewModel
{
    public SelectList ResortsSelectList { get; set; }
    [Required]
    [DisplayName("Resorts")]
    public List<string> ListResort { get; set; }

    public List<Rules> ExtraRuleList { get; set; }


    public OperaRateImportRuleViewModel()
    {
        ExtraRuleList = new List<Rules>();

        ListResort = new List<string>();
    }
}
public ActionResult EditRules(string id)
    {
        OperaRateImportRuleViewModel model = new OperaRateImportRuleViewModel();
        var getRulesForResort = service.GetAllOperaRateRules().Where(x => x.ResortCode == id).ToList();
        foreach (var item in getRulesForResort)
        {
            var ruleModel = new Rules()
            {
                Id = item.Id,
                IsReferenceRule = item.IsReferenceRule,
                PercentVariation = item.PercentVariation == null ? 0 : Decimal.Round(item.PercentVariation.Value, 2),
                RateCode = item.RateCode,
                ResortCode = item.ResortCode,
                RoomType = item.RoomType,
                SingleRoomDifference = item.SingleRoomDifference == null ? 0 : Decimal.Round(item.SingleRoomDifference.Value, 2),
                SupplementValue = item.SupplementValue == null ? 0 : Decimal.Round(item.SupplementValue.Value, 2)
            };
            model.ExtraRuleList.Add(ruleModel);
        }
        return View(model);
    }

    [HttpPost]
    public ActionResult EditRules(OperaRateImportRuleViewModel model)
    {
        foreach (var item in model.ExtraRuleList)
        {
            var rule = service.GetAllOperaRateRules().Where(x => x.Id == item.Id).FirstOrDefault();
            rule.RateCode = item.RateCode;
            rule.ResortCode = item.ResortCode;
            rule.RoomType = item.RoomType;
            rule.PercentVariation = item.PercentVariation;
            rule.SupplementValue = item.SupplementValue;
            rule.SingleRoomDifference = item.SingleRoomDifference;
            rule.IsReferenceRule = item.IsReferenceRule;
            service.Edit(rule);
        }
        return RedirectToAction("ManageRules");
    }
for (int i = 0; i < Model.ExtraRuleList.Count; i++)
{
    @Html.HiddenFor(x => Model.ExtraRuleList[i].Id)
    <div class="row">
     <div class="col-md-2">
         <div class="form-group">
             @Html.TextBoxFor(x => x.ExtraRuleList[i].ResortCode, new { @class = "form-control" })
         </div>
     </div>
    <div class="col-md-2">
             <div class="form-group">
                 @Html.TextBoxFor(x => x.ExtraRuleList[i].ResortCode, new { @class = "form-control" })
             </div>
         </div>
         <div class="col-md-2">
             <div class="form-group">
                 @Html.TextBoxFor(x => x.ExtraRuleList[i].RoomType, new { @class = "form-control" })
             </div>
         </div>
         <div class="col-md-1">
             <div class="form-group">
                 @Html.TextBoxFor(x => x.ExtraRuleList[i].PercentVariation, new { @class = "form-control textBoxSize" })
             </div>
         </div>
         <div class="col-md-1">
             <div class="form-group">
                 @Html.TextBoxFor(x => x.ExtraRuleList[i].SupplementValue, new { @class = "form-control textBoxSize" })
             </div>
         </div>
         <div class="col-md-1">
             <div class="form-group">
                 @Html.TextBoxFor(x => x.ExtraRuleList[i].SingleRoomDifference, new { @class = "form-control textBoxSize" })
             </div>
         </div>
         <div class="col-md-1">
             <div class="form-group">
                 @Html.LabelFor(m => m.ExtraRuleList[i].IsReferenceRule, new { @class = "checkbox-label" })
                 @Html.CheckBoxFor(x => x.ExtraRuleList[i].IsReferenceRule)
             </div>
         </div>
         <div class="col-xs-2">
             <div class="form-group">
                 <span id="deleteSeason" title="Delete" onclick="$(this).closest('.row').remove().trigger(review());" class="glyphicon glyphicon-remove text-danger row-action"></span><span>&nbsp;&nbsp;&nbsp;</span>
             </div>
         </div>
</div>
}
<input type="submit" value="Edit" class="btn btn-primary" />
<a href="javascript:window.history.back()" class="btn btn-primary">Back</a>
 public class Rules
{
    public int Id { get; set; }
    [DisplayName("Rate Code")]
    public string RateCode { get; set; }
    [DisplayName("Resort Code")]
    public string ResortCode { get; set; }
    [DisplayName("Room Type")]
    public string RoomType { get; set; }
    [DisplayName("Percent Variation")]
    public decimal? PercentVariation { get; set; }
    [DisplayName("Supplement Value")]
    public decimal? SupplementValue { get; set; }
    [DisplayName("Single Room Difference")]
    public decimal? SingleRoomDifference { get; set; }
    [DisplayName("")]
    public bool IsReferenceRule { get; set; }

    public string HotelString { get; set; }
}
最后是我的观点:

public class OperaRateImportRuleViewModel
{
    public SelectList ResortsSelectList { get; set; }
    [Required]
    [DisplayName("Resorts")]
    public List<string> ListResort { get; set; }

    public List<Rules> ExtraRuleList { get; set; }


    public OperaRateImportRuleViewModel()
    {
        ExtraRuleList = new List<Rules>();

        ListResort = new List<string>();
    }
}
public ActionResult EditRules(string id)
    {
        OperaRateImportRuleViewModel model = new OperaRateImportRuleViewModel();
        var getRulesForResort = service.GetAllOperaRateRules().Where(x => x.ResortCode == id).ToList();
        foreach (var item in getRulesForResort)
        {
            var ruleModel = new Rules()
            {
                Id = item.Id,
                IsReferenceRule = item.IsReferenceRule,
                PercentVariation = item.PercentVariation == null ? 0 : Decimal.Round(item.PercentVariation.Value, 2),
                RateCode = item.RateCode,
                ResortCode = item.ResortCode,
                RoomType = item.RoomType,
                SingleRoomDifference = item.SingleRoomDifference == null ? 0 : Decimal.Round(item.SingleRoomDifference.Value, 2),
                SupplementValue = item.SupplementValue == null ? 0 : Decimal.Round(item.SupplementValue.Value, 2)
            };
            model.ExtraRuleList.Add(ruleModel);
        }
        return View(model);
    }

    [HttpPost]
    public ActionResult EditRules(OperaRateImportRuleViewModel model)
    {
        foreach (var item in model.ExtraRuleList)
        {
            var rule = service.GetAllOperaRateRules().Where(x => x.Id == item.Id).FirstOrDefault();
            rule.RateCode = item.RateCode;
            rule.ResortCode = item.ResortCode;
            rule.RoomType = item.RoomType;
            rule.PercentVariation = item.PercentVariation;
            rule.SupplementValue = item.SupplementValue;
            rule.SingleRoomDifference = item.SingleRoomDifference;
            rule.IsReferenceRule = item.IsReferenceRule;
            service.Edit(rule);
        }
        return RedirectToAction("ManageRules");
    }
for (int i = 0; i < Model.ExtraRuleList.Count; i++)
{
    @Html.HiddenFor(x => Model.ExtraRuleList[i].Id)
    <div class="row">
     <div class="col-md-2">
         <div class="form-group">
             @Html.TextBoxFor(x => x.ExtraRuleList[i].ResortCode, new { @class = "form-control" })
         </div>
     </div>
    <div class="col-md-2">
             <div class="form-group">
                 @Html.TextBoxFor(x => x.ExtraRuleList[i].ResortCode, new { @class = "form-control" })
             </div>
         </div>
         <div class="col-md-2">
             <div class="form-group">
                 @Html.TextBoxFor(x => x.ExtraRuleList[i].RoomType, new { @class = "form-control" })
             </div>
         </div>
         <div class="col-md-1">
             <div class="form-group">
                 @Html.TextBoxFor(x => x.ExtraRuleList[i].PercentVariation, new { @class = "form-control textBoxSize" })
             </div>
         </div>
         <div class="col-md-1">
             <div class="form-group">
                 @Html.TextBoxFor(x => x.ExtraRuleList[i].SupplementValue, new { @class = "form-control textBoxSize" })
             </div>
         </div>
         <div class="col-md-1">
             <div class="form-group">
                 @Html.TextBoxFor(x => x.ExtraRuleList[i].SingleRoomDifference, new { @class = "form-control textBoxSize" })
             </div>
         </div>
         <div class="col-md-1">
             <div class="form-group">
                 @Html.LabelFor(m => m.ExtraRuleList[i].IsReferenceRule, new { @class = "checkbox-label" })
                 @Html.CheckBoxFor(x => x.ExtraRuleList[i].IsReferenceRule)
             </div>
         </div>
         <div class="col-xs-2">
             <div class="form-group">
                 <span id="deleteSeason" title="Delete" onclick="$(this).closest('.row').remove().trigger(review());" class="glyphicon glyphicon-remove text-danger row-action"></span><span>&nbsp;&nbsp;&nbsp;</span>
             </div>
         </div>
</div>
}
<input type="submit" value="Edit" class="btn btn-primary" />
<a href="javascript:window.history.back()" class="btn btn-primary">Back</a>
 public class Rules
{
    public int Id { get; set; }
    [DisplayName("Rate Code")]
    public string RateCode { get; set; }
    [DisplayName("Resort Code")]
    public string ResortCode { get; set; }
    [DisplayName("Room Type")]
    public string RoomType { get; set; }
    [DisplayName("Percent Variation")]
    public decimal? PercentVariation { get; set; }
    [DisplayName("Supplement Value")]
    public decimal? SupplementValue { get; set; }
    [DisplayName("Single Room Difference")]
    public decimal? SingleRoomDifference { get; set; }
    [DisplayName("")]
    public bool IsReferenceRule { get; set; }

    public string HotelString { get; set; }
}

由于
IsReferenceRule
属性用
[DisplayName(“”)]
修饰,因此引发了错误。例如,您需要删除它,或者给它一个值

[DisplayName("Is Reference Rule ")]
public bool IsReferenceRule { get; set; }
但在任何情况下,都应该使用
DisplayAttribute
,而不是
DisplayNameAttribute

[Display(Name = "Is Reference Rule ")]


具体来说,当调用
DataAnnotationsModelValidator
public override IEnumerable Validate(对象容器)
方法时,会发生错误。中有问题的行是
context.DisplayName=Metadata.GetDisplayName()
返回
null
,因为属性中缺少值。

我认为视图正常。规则列表的for each是否包装在表单中?我假设只有在单击“删除”按钮(span)时绑定才会失败@StephenMuecke当我按下提交按钮时绑定才会失败button@Wheels73是的,绑定在
IsReferenceRule
属性上的for each…Remove
[DisplayName(“”)]
中不起作用(或包括一个值-例如,
[DisplayName(“Is参考规则”)]