简体   繁体   English

列表中的模型绑定

[英]Model Binding in List

I have a list inside ViewModel but when I make the Post to the Controller it's not binding it and shows an error on Parameter: null, Value: Null. 我在ViewModel中有一个列表但是当我向控制器发布时它没有绑定它并且在参数上显示错误:null,值:Null。

If you could help me out on this. 如果你能帮我解决这个问题。

ViewModel: 视图模型:

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>();
    }
}

Controller: 控制器:

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");
    }

And finally my View: 最后我的观点:

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>

Error : Error when Binding 错误绑定时出错

Much appreciated! 非常感激!

Thanks :) 谢谢 :)

EDIT ViewModel Rules: 编辑 ViewModel规则:

 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; }
}

Your error is thrown because your IsReferenceRule property is decorated with [DisplayName("")] . 抛出您的错误,因为您的IsReferenceRule属性使用[DisplayName("")]修饰。 You need to either delete it, or give it a value, for example 例如,您需要删除它,或者给它一个值

[DisplayName("Is Reference Rule ")]
public bool IsReferenceRule { get; set; }

but in any case, you should be using the DisplayAttribute , not the DisplayNameAttribute 但无论如何,您应该使用DisplayAttribute ,而不是DisplayNameAttribute

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

Specifically, the error occurs when the public override IEnumerable<ModelValidationResult> Validate(object container) method of DataAnnotationsModelValidator is called. 具体而言,当调用DataAnnotationsModelValidatorpublic override IEnumerable<ModelValidationResult> Validate(object container)方法时, public override IEnumerable<ModelValidationResult> Validate(object container)发生错误。 The offending line in the source code is context.DisplayName = Metadata.GetDisplayName(); 源代码中的违规行是context.DisplayName = Metadata.GetDisplayName(); which returns null because of the missing value in the attribute. 由于属性中缺少值而返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM