ViewModel中的MVC Collection在回发时始终为null

时间:2018-03-12 14:30:31

标签: c# asp.net-mvc-5

我有这个ViewModel:

public class DepositViewModel
{

    public DepositViewModel()
    {
        DepositDate = DateTime.Now;
        CollectedReceipts = new List<DepositQuantityAmountViewModel>();
        CancelledReceipts = new List<DepositQuantityAmountViewModel>();
    }

    [Display(Name = "Deposit #")]
    public long DepositId { get; set; }

    [Display(Name = "Deposit Type")]
    public string DepositType { get; set; }

    [Display(Name = "Cashier #")]
    public int CashierId { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Deposit Date")]
    [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime DepositDate { get; set; }

    [Display(Name = "Collected Receipts")]
    public IList<DepositQuantityAmountViewModel> CollectedReceipts { get; set; }

    [Display(Name= "Cancelled Receipts")]
    public IList<DepositQuantityAmountViewModel> CancelledReceipts { get; set; }

    [Display(Name = "Finance Reference")]
    [MaxLength(2000)]
    public string FinanceReference { get; set; }

    [Display(Name = "Received Amount")]
    [DisplayFormat(DataFormatString = "{0:N2}", ApplyFormatInEditMode = true)]
    public decimal ReceivedAmount { get; set; }

    [Display(Name = "Payment Modes")]
    public IList<BDOs.PayMode> PaymentModes { get; set; }
}

public class DepositQuantityAmountViewModel
{
    [Display(Name = "Description")]
    public string Description { get; set; }

    [Display(Name = "Category")]
    public string Category { get; set; }

    [Display(Name = "Count")]
    public int Count { get; set; }

    [Display(Name = "Total")]
    [DisplayFormat(DataFormatString = "{0:N2}", ApplyFormatInEditMode = true)]
    public decimal Amount { get; set; }
}

在我看来,我有这样的设置:

<div class="row">
    <div class="col-sm-2">
        <div class="form-group">
            <fieldset>
                <legend>@Html.DisplayNameFor(m => m.PaymentModes)</legend>
                <div class="col-sm-12">
                    <div class="row">
                        @foreach (var modelPaymentMode in Model.PaymentModes)
                        {
                            @Html.DisplayFor(m => modelPaymentMode, "_DepositPartPaymentModes")
                        }
                    </div>
                </div>
            </fieldset>
        </div>
    </div>

    <div class="col-sm-5">
        <div class="form-group">
            <fieldset>
                <legend>@Html.DisplayNameFor(m => m.CollectedReceipts)</legend>
                @Html.DisplayFor(m => m.CollectedReceipts, "_DepositPartDetail")
            </fieldset>
        </div>
    </div>

    <div class="col-sm-5">
        <div class="form-group">
            <fieldset>
                <legend>@Html.DisplayNameFor(m => m.CancelledReceipts)</legend>
                @Html.DisplayFor(m => m.CancelledReceipts, "_DepositPartDetail")
            </fieldset>
        </div>
    </div>
</div>

以下是显示模板:

_DepositPartPaymentModes.cshtml

@model DA.Services.IBS.Business.BDO.PayMode

<div class="form-group">
    @Html.TextBoxFor(m => m.Name, new { @class = "form-control", @readonly = "readonly" })
</div>

_DepositPartDetail.cshtml

@model IEnumerable<DA.Services.IBS.Web.FinancePortalFull.Models.DepositQuantityAmountViewModel>

@foreach (var countAmountPair in Model)
{
    if (countAmountPair.Category == "TotalCollected" || countAmountPair.Category == "TotalCancelled")
    {
        <div class="col-sm-12">
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        @Html.TextBoxFor(m => countAmountPair.Description, new { @class = "form-control", @readonly = "readonly" })
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        @Html.TextBoxFor(m => countAmountPair.Amount, "{0:N2}", new { @class = "form-control", type = "numeric", dir = "rtl", @readonly = "readonly" })
                    </div>
                </div>
            </div>
        </div>
    }
    else
    {
        <div class="col-sm-12">
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        @Html.TextBoxFor(m => countAmountPair.Count, "{0:N0}", new { @class = "form-control", type = "numeric", dir = "rtl", @readonly = "readonly" })
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        @Html.TextBoxFor(m => countAmountPair.Amount, "{0:N2}", new { @class = "form-control", type = "numeric", dir = "rtl", @readonly = "readonly" })
                    </div>
                </div>
            </div>
        </div>
    }
}

我试图分别绑定每个模板,查看不同的指南。我无法将集合返回到回发,因为它们为null。它们正确显示所有三个集合在提交时都为空。

我做错了什么?

解决方案 不要在视图中使用ForEach循环来迭代集合。使用For循环。

2 个答案:

答案 0 :(得分:2)

问题在于使用foreach循环。当在HTML中呈现时,输入元素的名称不会在发布时对模型绑定器起任何意义。您需要将这些更改为for循环,并且输入将正确呈现。

您可能还需要更改模型,以便在活页夹尝试添加时,列表不为空。

例如:

@for (int i = 0; i < Model.PaymentModes; i++)
{
    @Html.DisplayFor(m => Model.PaymentModes[i], "_DepositPartPaymentModes")
}

答案 1 :(得分:0)

使用using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HelloWorldAgain { class Program { static void Main(string[] args) { string userName = ""; int userAge = 0; int currentYear = 0; Console.Write("Please enter your name: "); userName = Console.ReadLine(); Console.Write("Please enter your age: "); userAge = Convert.ToInt32(Console.ReadLine()); Console.Write("Please enter current year: "); currentYear = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Hello World! My name is {0} and I am {1} years old. I was born in {2}.", userName, userAge, currentYear - userAge); } } } 循环代替paramiko

元素应该是这样的。

for

foreach更改为<input type="text" name="List[0].PropertyName" value="XYZ" />

<强> _DepositPartDetail.cshtml

@model IEnumerable<DA.Services.IBS.Web.FinancePortalFull.Models.DepositQuantityAmountViewModel>