C#-传递视图模型数据

C#-传递视图模型数据,c#,html,asp.net-mvc,viewmodel,C#,Html,Asp.net Mvc,Viewmodel,我有一个post方法,其中包含我的视图模型及其所有数据 [HttpPost] [ValidateAntiForgeryToken] public ActionResult SpecialOrderSelection(JobOrder job, ItemViewModel model) { return RedirectToAction("SpecialOrderSummary", "JODetails", model);

我有一个post方法,其中包含我的视图模型及其所有数据

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SpecialOrderSelection(JobOrder job, ItemViewModel model)
{               
   return RedirectToAction("SpecialOrderSummary", "JODetails", model);        
}
“特殊订单选择”页面向用户显示零件列表,用户必须选择是否转移、收获或处置零件。它将正确的数据返回给控制器。但是在提交完成后,我想给用户一个他们所做的一切的摘要页面。为此,我尝试将视图模型传递到“订单摘要”页面。get方法如下所示

public ActionResult SpecialOrderSummary(ItemViewModel model)
{       
   return View(model);
}
@model PIC_Program_1._0.Models.ItemViewModel
@using PIC_Program_1._0.Models
@{
    ViewBag.Title = "SpecialOrderSummary";
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @*@Html.HiddenFor(model => model.ID)*@
    @Html.HiddenFor(x => x.ID)
    <h2>Special Order Summary</h2>
    <p style="color:red" class="noprint">Please review and verify </p>

    <h2>
        Transfers
    </h2>
    <h3> 
    Parts 
    </h3>
    foreach (var part in Model.Parts) // part is null here
    {
    <p>@part.PartName</p>
    }

}
这是我的模型

 public class ItemViewModel
    {
        [Required]
        public int ID { get; set; }
        public string ItemId { get; set; }
        public string ItemName { get; set; }
        public string MFGNumber { get; set; }
        public IList<ItemPartViewModel> Parts { get; set; }
        public IList<ItemComponentViewModel> Components{ get; set; }
        public IList<ComponentPartViewModel> ComponentParts { get; set; }
        public IList<ComponentSubCompViewModel> ComponentSubComps { get; set; }
        public IList<SubCompPartViewModel> SubCompParts { get; set; }

        public IList<SubCompSubCompViewModel> SubCompSubComps { get; set; }
        public IList<SubCompSubCompPartViewModel> SubCompSubCompParts { get; set; }

    }

我假设客户机在请求体中传递作业和模型

我将展示这个序列会发生什么:

您不必重定向客户端,只需返回oter操作的结果:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SpecialOrderSelection(JobOrder job, ItemViewModel model)
{               
   return SpecialOrderSummary(model);        
}

什么部分是空的?你能给我们看看你的模型吗?@derloopkat好的,我在模型里加了。我猜null可能是描述它的错误方式,Model.parts中没有数据。根据,如果您在
重定向到操作
中传递模型,这将适用于简单值,但对于复杂对象(例如集合)无效。当您发回
ItemViewModel
时,如果不将项目列表放入隐藏输入中,它们将为空!表单不会发回任何没有输入的内容。我的解决方案是使用Ajax,而不是使用常规表单post。这样您就不必重新获取视图模型中的所有内容。@DavidLiang我可以通过Ajax传递整个视图模型吗?它将数据发送到POST方法,但我希望能够移动该模型,因为我想给用户一个他们为确认其操作所做的总结感谢该解释!奇怪的是,它似乎把我踢回了刚才的页面。我在那个页面上得到了一个空异常错误,因为它似乎在重新加载同一个页面,但是视图类的模型不同。古怪的也许这只是VisualStudio的一个bug,你能添加action SpeciallorderSummary的代码吗?你是什么意思?我不是已经在我的问题中添加了这一点吗?还是你在说别的什么我的错!我错过了。问题在于,该模型与您在SpecialLordersElection方法开始时所期望的不同。如果您可以显示原始请求负载(F12,然后是浏览器上的“网络”选项卡),我们可以尝试了解它不工作的原因。不幸的是,如果我的代码与您显示的方式相同,它将无法编译,因为它有空引用
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SpecialOrderSelection(JobOrder job, ItemViewModel model)
{               
   return SpecialOrderSummary(model);        
}