I am having the below PaymentInformationModel Class. Which is having a complex type CreditCardDetailModel. When I submit my form the CreditCartDetail property remain empty, I am expecting it to be filled with all the details entered by the user. Do I need to do custom binding or there is a default binding trick which I am missing.
PaymentInformationModel
public class PaymentInformationModel
{
public string PaymentAmount { get; set; }
public string TransactionReference { get; set; }
public string Description { get; set; }
public CreditCardDetailModel CreditCardDetail{get;set;}
}
CreditCardDetailModel
public class CreditCardDetailModel
{
public string CardNumber { get; set; }
public string Name { get; set; }
public string ExpiryDate { get; set; }
public int CardSecurityCode { get; set; }
public CreditCardType CardType { get; set; }
}
VIEW
@model PaymentInformationModel
@using (Html.BeginForm("", "Payment", FormMethod.Post, new { Id = "Form1", @class = "form-horizontal" }))
{
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">Payment Information</div>
<div class="panel-body">
<div class="form-group">
@Html.LabelFor(x => x.PaymentAmount, new { @class = "control-label col-sm-2" })
<div class="input-group col-sm-3">
<span class="input-group-addon">$</span>
@Html.TextBoxFor(m => m.PaymentAmount, new { @class = "form-control col-sm-10" })
</div>
@Html.ValidationMessageFor(m => m.PaymentAmount, "", new { @class = "help-block" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.TransactionReference, new { @class = "control-label col-sm-2" })
@Html.TextBoxFor(t => t.TransactionReference, new { @class = "form-control col-sm-10" })
</div>
<div class="form-group">
@Html.LabelFor(l => l.Description, new { @class = "control-label col-sm-2" })
@Html.TextAreaFor(t => t.Description, new { @class = "form-control col-sm-10" })
</div>
</div>
</div>
@Html.Action("CreditCardDetail")
<p class="log"></p>
</div>
<button type="submit" name="btnSubmit" id="btnSubmit" class="btn btn-success">PAY</button>
}
Controller
[HttpPost]
public ActionResult Index(PaymentInformationModel model)
{
if (ModelState.IsValid)
{
return View();
}
return View();
}
public PartialViewResult CreditCardDetail()
{
return PartialView("CreditCardDetail_Partial");
}
CreditCardDetailproperty has a getter only. TheDefaultModelBindercannot set it. Change the property topublic CreditCardDetailModel CreditCardDetail { get; set; }CreditCardDetailModelhave a property for anotherCreditCardDetailModel?@Html.Action("CreditCardDetail")return? (show the relevant code) - Unless its generating the correctly prefixednameattributes, then it will not bindname="CreditCardDetail.CardNumber",name="CreditCardDetail.ExpiryDate"etc. And why are you using@Html.Action()for this. The correct approach is to use anEditorTemplatefor typeofCreditCardDetailModel