How to pass the model items between MVC Actions

c# asp.net-mvc asp.net-mvc-4

75 观看

2回复

642 作者的声誉

I want create a Master/Detail page that shows properties of the model as well items of properties of the same model that are collections. The page itself should only have one save button, that stores the values in a database. I also want to allow the the user to make changes to the collection properties, that are shown on the page without saving them into the database. The following code shows the setup for the picture collection, but I also want to do this for a "Child-table/grid" i.e. collection of "pocos". Is there a way to do this in MVC? To my understanding, I would have to keep the instance of the object and pass it between the HTMLActions, as this instance holds all the changes. Just some pointers in the right direction would be nice or, if the case, pointing out, that MVC should not be used for this...

The model:

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

Controller:

    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            
    }

View:

@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>

Update 06.01.2018:

What works in MVC5 is to use the sessionString. However, I've learned that this won't work in asp.net Core.

Set:

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

Get:

System.Web.HttpContext.Current.Session["sessionString"] = m;
作者: Mister 832 的来源 发布者: 2017 年 12 月 27 日

回应 2


-1

44 作者的声誉

Try TempData to store your Data and access in next Request.

作者: Uttam Suthar 发布者: 2017 年 12 月 29 日

1

217502 作者的声誉

决定

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

Pure MVC won't cut it, and you're already on your way with the Ajax calls.
But you'll find that that gets more and more complicated.

The best route would be to study up on SPA, with for instance Angular.

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

Yes, but that is server-side state manangment, problems with scale-out etc.
But usable, for ASP.NET Core you could use the MemoryCache, or step up to ReDis. You still have (can configure) a Session Id.

With a SPA you won't need the cache/session so much, just use it for optimization.

作者: Henk Holterman 发布者: 2018 年 1 月 6 日
32x32