列表

时间:2017-01-31 11:04:56

标签: c# asp.net-mvc binding

我在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>

错误Error when Binding

非常感谢!

谢谢:)

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

1 个答案:

答案 0 :(得分:2)

您的错误已被抛出,因为您的IsReferenceRule媒体资源已被[DisplayName("")]修饰。您需要删除它,或者给它一个值,例如

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

但无论如何,您应该使用DisplayAttribute,而不是DisplayNameAttribute

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

具体而言,调用public override IEnumerable<ModelValidationResult> Validate(object container) DataAnnotationsModelValidator方法时会发生错误。由于属性中缺少值,source code中的违规行为context.DisplayName = Metadata.GetDisplayName();,返回null