无法在MVC中绑定Complex类型

时间:2016-07-08 00:56:48

标签: c# asp.net-mvc

我有以下的PaymentInformationModel类。具有复杂类型的CreditCardDetailModel。当我提交表单时,CreditCartDetail属性保持为空,我希望它填充用户输入的所有详细信息。我是否需要进行自定义绑定,或者我缺少一个默认的绑定技巧。

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


    }

查看

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

1 个答案:

答案 0 :(得分:1)

您对@Html.Action()的使用是调用一个控制器方法,该方法返回CreditCardDetailModel的部分视图,该视图将生成具有名称属性的输入,例如

<input name="CardNumber" .... />
<input name="ExpiryDate" .... />

但是为了绑定到你的模型,他们需要

<input name="CreditCardDetail.CardNumber" .... />
<input name="CreditCardDetail.ExpiryDate" .... />

不清楚为什么使用@Html.Action()来调用不包含逻辑的服务器方法,并且您可以轻松使用@Html.Partial("CreditCardDetail_Partial"),尽管这仍然会导致同样的错误name属性。

您需要EditorTemplate使用CreditCardDetailModel类型。将您的CreditCardDetail_Partial.cshtml文件移至/Views/Shared/EditorTemplates文件夹,并将其重命名为CreditCardDetailModel.cshtml(即匹配班级名称)。然后在视图中,使用

@Html.EditorFor(m => m.CreditCardDetail)

将生成用于绑定到模型的正确name属性。