通过AJAX将视图模型传递到另一个视图页面

时间:2020-07-10 14:55:02

标签: javascript c# jquery ajax asp.net-mvc

我的用户当前有一个视图,他们必须在其中选择每个零件的状态。 (收获/转移/处置)。在用户做出选择之后,我希望为他们提供刚刚选择的内容的摘要页面。

这是我对摘要页面的看法

@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)
    {
    <p>@part.PartName</p>
    }

}

我尝试过POST,然后调用了如下所示的摘要页面

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SpecialOrderSelection(JobOrder job, ItemViewModel model)
        {
            //list of transfers
            //list of harvests
            //list of disposals      
            if (ModelState.IsValid)
            {
                // do whatever with 'model' and return or redirect to a View
            }
            return SpecialOrderSummary(model);

        }

这是我的ViewModel供参考,


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

    }

但是这不起作用,因为它只是再次重新加载了相同的“ SpecialOrderSelection”页面。我已经读过,最好是通过AJAX发布ViewModel,而不是那样进行表单发布,这样我就不必重新获取ViewModel中的所有数据。但是我不确定如何实现这一目标。任何帮助表示赞赏。

这里是我尝试做的事情

 $(document).ready(function () {
        $("button").click(function () {
            $.ajax({
                url: '@IGT.baseUrl/JODetails/SpecialOrderSummary',
                data: $('#form').serialize(),
                type: 'POST',
            });
        });
     }); //getting green line that says ',' expected on this line

0 个答案:

没有答案