ViewModel,View和PartialView发布到控制器

时间:2019-01-12 05:15:20

标签: asp.net-mvc

我正在尝试执行以下操作:我有两个模型,标头和列表(详细信息),它们是由视图模型发送到视图的。加载主视图时,将从先前加载的ViewModel.header模型的列表中显示一个下拉列表。当您单击该下拉菜单时,将向局部视图加载一些值,这些值由ViewModel.List(details)的ddl值过滤,以供用户完成信息。到目前为止,一切正常,但是在执行Post控制器时,它会收到null的ViewModel.List(details)。

我在做什么错了?

标题

    public class StockTransactionsHeader
{
    [Key]
    public int TransactionHeaderID { get; set; }

    public DateTime TransactionDate { get; set; }

    public string TransactionDocument { get; set; }

    public int CategoryID { get; set; }

    [NotMapped]
    public List<SelectList> CategoryCollection { get; set; }

    public virtual List<StockTransactionsDetails> StockTransactionsDetails { get; set; }

}

详细信息

    public class StockTransactionsDetails
{
    [Key]
    public int TransactionDetailID { get; set; }

    public int TransactionHeaderID { get; set; }

    public int ProductID { get; set; }

    public decimal Qty { get; set; }

    public decimal Amount { get; set; }

    public decimal TransactionAmount { get; set; }

    [NotMapped]
    public string ProductDescription { get; set; }

    public virtual StockTransactionsHeader StockTransactionsHeader { get; set; }
}

ViewModel

public class StockTransactionsViewModel
{
    public StockTransactionsHeader StockTransactionsHeader { get; set; }
    public List<StockTransactionsDetails> StockTransactionsDetails { get; set; }
}

创建控制器

 public ActionResult Create()
 {
     var stockTransactions = new StockTransactionsViewModel();
     stockTransactions.StockTransactionsHeader = GetHeaderCategories();

     return View(stockTransactions);
 }

GetHeaderCategories()

private StockTransactionsHeader GetHeaderCategories()
{
        var header = new StockTransactionsHeader();
        header.CategoryCollection = CommonServices.GetSelecList((int)DeliveryCommonHelper.ConfigurationType.Categoria);
        return header;
}

MainView

@model DeliverySolutionCommon.ViewModels.StockTransactionsViewModel
@using (Html.BeginForm())
{
    <div class="form-row">
        <div id="partialView" class="table-responsive">

        </div>
    </div>

    <div class="form-group">
        <div class="col-md-2">
            <input type="submit" value=" Procesar " class="btn btn-warning" />
        </div>
    </div>
}

用于加载部分视图的脚本

    <script>
    $(document).ready(function () {
        $("#Category").on("change", function () {
            autoFiltro();
        })
    })

    function autoFiltro() {
        var url = "@Url.Action("GetProductsListByCategory", "StockTransactions")";
        var id = $("#Category").val();
        var data = { idCategory: id };

        $.post(url, data).done(function (data) {
            $("#partialView").html(data);
        })
    }
</script>

GetProductsListByCategory

        [HttpPost]
    public PartialViewResult GetProductsListByCategory(int idCategory)
    {
        var products = ProductsServices.GetProductsListByCategory(idCategory);
        var stockTransactions = new StockTransactionsViewModel();
        stockTransactions.StockTransactionsDetails = GetTransactionsDetails(products);

        return PartialView("_createStockTransactions", stockTransactions);
    }

GetTransactionsDetails

        private List<StockTransactionsDetails> GetTransactionsDetails (List<Products> products)
    {
        var details = new List<StockTransactionsDetails>();

        foreach (var item in products)
        {
            StockTransactionsDetails detail = new StockTransactionsDetails();
            detail.ProductID = item.ProductID;
            detail.ProductDescription = item.Description;

            details.Add(detail);
        }
        return details;
    }

PartialView

    @model DeliverySolutionCommon.ViewModels.StockTransactionsViewModel
<table class="table table-sm table-bordered table-striped">
    @foreach (var item in Model.StockTransactionsDetails)
        {
            <tr class="d-flex">
                <td class="col-7">
                    @Html.DisplayFor(modelItem => item.ProductDescription)
                </td>
                <td class="col-1">
                    @Html.EditorFor(modelItem => item.Qty, new { htmlAttributes 
                    = new { @class = "form-control" } })
                </td>
                <td class="col-2">
                    @Html.EditorFor(modelItem => item.Amount, new { 
                     htmlAttributes = new { @class = "form-control" } })
                </td>
                <td class="col-2">
                    @Html.EditorFor(modelItem => item.TransactionAmount, new { 
                     htmlAttributes = new { @class = "form-control" } })
                </td>
            </tr>
        }
</table>

Aaaa,最后创建帖子

        [HttpPost]
    public ActionResult Create(StockTransactionsViewModel stockTransactionsView)
    {
       // StockStransactionsView.StockTransactionsDetails = null

    }

1 个答案:

答案 0 :(得分:0)

问题是您正在发回列表,并且HTML中没有索引信息... MVC模型绑定程序不知道如何将没有索引信息的项目放在列表中...

您可以尝试以下操作:

['Joe's Jazz Band (Ensemble)', 'Fred Dagg (Guitar)', 'Tommy Tucker (Horn, Clarinet)', 'The Pied Piper (Whistle)']

这会将索引信息添加到您的HTML ...

或者,您可以使用@for (int i = 0; i < Model.StockTransactionsDetails.Count, i++) { <tr class="d-flex"> <td class="col-7"> @Html.EditorFor(modelItem => Model[i].Amount, new { htmlAttributes = new { @class = "form-control" } }) </td> // more code... ...像这样:

EditorTemplate

This tutorial might help