简体   繁体   English

ASP.NET MVC ViewModel始终为null @ html.displayfor

[英]ASP.NET MVC ViewModel always null @html.displayfor

When I bind my model using @Html.DisplayFor(), it says that the "value cannot be null or empty". 当我使用@ Html.DisplayFor()绑定模型时,它表示“值不能为null或为空”。

Did the following 做了以下

  1. I checked that the controller passes a valid model to the view. 我检查了控制器是否将有效模型传递给视图。

  2. Made sure that the parameters have the corresponding {get;set;}. 确保参数具有相应的{get; set;}。

  3. Made an @if(Model != null && Model.Param != "") in the view 在视图中创建一个@if(Model!= null && Model.Param!=“”)

Funny thing is that it works in the AdminArea and not in the StoreArea. 有趣的是,它可以在AdminArea中工作,而不能在StoreArea中工作。 Both areas are using the same model 两个区域都使用相同的模型

Edit: 编辑:

    @using System.Web.UI.HtmlControls
    @using SU.Plugin.Payments.BankDeposit.Models.

    @{
        Layout = "~/Views/Shared/Master.cshtml";
     }

    @model OrderPaymentModel
  <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <h2>Payment Form</h2>
                @if (Model.Status == PaymentStatus.Invalid)
                {
                    @Html.ActionLink("Edit payment details", "OrderPayment", new {orderId = Model.OrderId, mode = "edit"})
                }
                <div class=custom-page-box-div>
                    <div class="fieldset">
                        <div class="form-fields">
                            <div class="form-group row">
                                <div class="col-md-2">
                                    <label class="col-md- control-label"> @Html.LabelFor(model => model.BankPaidTo)</label>
                                </div>
                                <div class="col-md-8">
                                    @Html.DisplayFor(c => c.BankPaidTo.DisplayName)
                                    @Html.DisplayFor(model => model.BankPaidTo.DisplayName)
                                </div>
                        </div>
                        <div class="form-group row">
                            <div class="col-md-2">
                                @Html.LabelFor(model => model.TransactionNumber)
                            </div>
                            <div class="col-md-8">
                                @Html.DisplayFor(model => model.TransactionNumber)
                            </div>
                        </div>

                        <div class="form-group row">
                            <div class="col-md-2">
                                @Html.LabelFor(model => model.PaidAmount)
                            </div>
                            <div class="col-md-8">
                                @Html.DisplayFor(model => model.PaidAmount)
                            </div>
                        </div>

                        <div class="form-group row">
                            <div class="col-md-2">
                                @Html.LabelFor(model => model.DatePaidDay)
                            </div>
                            <div class="col-md-8 datepicker-dropdowns">

                                @Html.DisplayFor(model => model.DateOfTransaction)
                            </div>
                        </div>
                        <div class="form-group row">
                            <div class="col-md-2">
                                Deposit Slip
                            </div>
                            <div class="col-md-8">
                                <img src="@Url.Content(Model.DepositSlipUrl)">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

Finally got it. 终于明白了。

So I tried to remove the DisplayFor's one by one which each removal causing the next parameter to be highlighted with the error. 因此,我尝试一个一个地删除DisplayFor的对象,每次删除都会导致下一个参数突出显示并显示错误。 (except for the one that was actually causing the problem. => (除了实际上引起问题的那一个。=>

It was only until i noticed the "contentPath" line in the error message and searched it up when I saw what was really causing my problems. 直到我注意到错误消息中的“ contentPath”行,并在发现真正引起问题的原因时对其进行搜索。

I don't under stand why it skipped that line (binding errors started from the bottom up). 我不明白为什么它会跳过那行(绑定错误从下而上开始)。 but it's fixed now so.. yey 但是现在固定了..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM