아래 PaymentInformationModel 클래스가 있습니다. 어느 쪽이 복합 형 CreditCardDetailModel을 가지고 있습니다. 양식을 제출할 때 CreditCartDetail 속성이 비어있는 채로 남아 있으며 사용자가 입력 한 모든 세부 정보로 채워질 것으로 예상됩니다. 사용자 지정 바인딩을 수행해야합니까, 아니면 내가 빠뜨린 기본 바인딩 트릭이 있습니까?MVC에서 복잡한 유형을 바인딩 할 수 없습니다.
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>
}
컨트롤러
[HttpPost]
public ActionResult Index(PaymentInformationModel model)
{
if (ModelState.IsValid)
{
return View();
}
return View();
}
public PartialViewResult CreditCardDetail()
{
return PartialView("CreditCardDetail_Partial");
}
'CreditCardDetail' 속성에는 getter 만 있습니다. DefaultModelBinder는 ** 설정할 수 없습니다 **. 속성을 'public CreditCardDetailModel CreditCardDetail {get; 세트; }' –
당신의'CreditCardDetailModel'은 다른'CreditCardDetailModel'에 대한 속성을 가지고있는 이유는 무엇입니까? –
코드를 업데이트했습니다. – maxspan