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.
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> </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:
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("")]. 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
[Display(Name = "Is Reference Rule ")]
Specifically, the error occurs when the public override IEnumerable<ModelValidationResult> Validate(object container) method of DataAnnotationsModelValidator is called. The offending line in the source code is context.DisplayName = Metadata.GetDisplayName(); which returns null because of the missing value in the attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With