简体   繁体   中英

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".

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

  3. Made an @if(Model != null && Model.Param != "") in the view

Funny thing is that it works in the AdminArea and not in the 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. (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.

I don't under stand why it skipped that line (binding errors started from the bottom up). but it's fixed now so.. yey

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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