如何在 MVC 操作之间传递模型项

How to pass the model items between MVC Actions

我想创建一个 Master/Detail 页面来显示模型的属性以及作为集合的同一模型的属性项。页面本身应该只有一个保存按钮,将值存储在数据库中。我还希望允许用户更改页面上显示的集合属性,而无需将它们保存到数据库中。下面的代码显示了图片集的设置,但我也想为 "Child-table/grid" 做这个,即 "pocos" 的集合。有没有办法在 MVC 中做到这一点? 据我了解,我必须保留对象的实例并将其传递到 HTMLActions 之间,因为该实例包含所有更改。 只是一些正确方向的指示会很好​​,或者,如果是这样的话,指出 MVC 不应该用于此...

型号:

    public class MasterModel : ModelBase
    {
        public MasterModel()
        {

        }

        private int id;

        public int Id
        {
            get { return id; }
            set { id = value;  }
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private ListBase<PicModel> pics;

        public ListBase<PicModel> Pics
        {
            get { return pics; }
            set { pics = value;  }
        }
    }

控制器:

    public ActionResult Edit(int id)
    {
        if (id <= 0 )
        {
            return RedirectToAction("Index");
        }

        m = new MasterModel (id);
        return View(m);
    }

    [HttpPost]
    public ActionResult NewPic(int id, HttpPostedFileBase uploadFile)
    {
        PicModel p = new PicModel();
        MemoryStream ms = new MemoryStream();
        uploadFile.InputStream.CopyTo(ms);
        b.Picture= ms.ToArray(); 

        m.Pics.Add(b); //Here it fails, as the MasterModel m is a different one then when the ActionResult Edit is called            
    }

查看:

@model app1.Models.MasterModel 

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/index.js")" type="text/javascript"></script>

<script>
$("#PicForm").on("submit", function (e) {
    e.preventDefault();

    var form = $(this);
    var formData = new FormData(form.get(0));

    $.ajax({
        url: form.attr("action"),
        method: form.attr("method"),
        data: formData,
        processData: false,
        contentType: false
    })
});

</script>


    <div class="col-md-4 col-lg-4">
        @using (Html.BeginForm("NewPic", "MasterModel ", FormMethod.Post, new { id = "PicForm", enctype = "multipart/form-data" }))
        {
            @Html.HiddenFor(model => model.Id)               

            <div class="container-fluid">
                @foreach (app1.Models.PicModel b in Model.Pics)
                {

                    var base64 = Convert.ToBase64String(b.Picture);
                    var imgSrc = String.Format("data:image/gif;base64,{0}", base64);

                    <img src="@imgSrc" width="200" height="200" />
                }
            </div>

            <div>
                <input type="file" id="uploadFile" name="uploadFile" />
                <input type="submit" value="uploadFile" class="submit" />
            </div>

        }
    </div>

2018 年 1 月 6 日更新:

在 MVC5 中有效的是使用 sessionString。但是,我了解到这在 asp.net Core 中不起作用。

设置:

m = (MasterModel )System.Web.HttpContext.Current.Session["sessionString"];

得到:

System.Web.HttpContext.Current.Session["sessionString"] = m;

尝试使用 TempData 存储您的数据并在下一个请求中访问。

or, ..., that MVC should not be used for this...

纯 MVC 不会削减它,您已经在进行 Ajax 调用。
但是你会发现这变得越来越复杂。

最好的途径是学习 SPA,例如 Angular。

What works in MVC5 is to use the Session[].

是的,但这是服务器端状态管理、横向扩展等问题
但是可用,对于 ASP.NET Core,您可以使用 MemoryCache,或者升级到 ReDis。您仍然拥有(可以配置)会话 ID。

使用 SPA,您将不需要那么多 cache/session,只需将其用于优化。