0

I apologize for my very poor terminology,

but I'll try to explain myself better:

  1. give a model that contains a list of obj and an obj

  2. I want a form in my view to send the obj (and the list )to the controller

  3. the controller to add the obj to the updated list and send it back to the view.

    my problem is passing the "initial" list to the form on submit;

this doesn't work:

 <div class="form-group">
  @Html.HiddenFor(m => m.RequestList, Model.RequestList)
</div>

Edit: answering to...

This question may already have an answer here:

Model Model Binding to a List MVC 4 3 answers

no it isn't, I'm not trying to edit the list rather than just "declare it", anyway as suggested I'm trying to loop the elements in the list and "add it to the new one";

  • something get actually passed to the model, but is null!

                        for (int i = 0; i < Model.RequestList.Count(); ++i)
                    {
                        @Html.HiddenFor(m => m.RequestList[i], new {
                            StartTime = Model.RequestList[i].StartTime,
                            EndTime = Model.RequestList[i].EndTime,
                            StatusId = Model.RequestList[i].StatusId,
                        })
                    }
    

    again: what am I doing wrong?

    • I think this solution is actually closer to what I mean (sorry isn't stackoverflow)

but trying something like this

@using (Html.BeginForm("CreateComplex",  null, FormMethod.Post, new { @class = "",  RequestList = Model.RequestList}))

still doesn't work

Francesco Iapicca
  • 2,618
  • 5
  • 40
  • 85
  • Possible duplicate of [Model Binding to a List MVC 4](https://stackoverflow.com/questions/15375800/model-binding-to-a-list-mvc-4) – Georg Patscheider Jul 12 '19 at 08:30
  • In short, you will need a `for` loop and then render hidden inputs for every property of the list items you want to bind back. Maybe it would be easier to add the new item to the list in the database, then reload the whole list from the backend instead of roundtripping it with POST? This way will you only need to POST the ID of the new item that has to be added. – Georg Patscheider Jul 12 '19 at 08:34
  • updated the main post, something get passed, but it's null, probably I'm doing it wrong; out of curiosity, since I don't aim to modify the list in the view, isn't there a way to just "pass it" to the new model without the need to loop? – Francesco Iapicca Jul 12 '19 at 10:22
  • Regarding the `null` being posted: your code should be `for (int i = 0; i < Model.RequestList.Count(); ++i) { @Html.HiddenFor(m => m.RequestList[i].StartTime) @Html.HiddenFor(m => m.RequestList[i].EndTime) @Html.HiddenFor(m => m.RequestList[i].StatusId) }`, i.e. one `@Html.HiddenFor` for every property of the model you want to post. – Georg Patscheider Jul 12 '19 at 12:58

2 Answers2

0

If you do not want to edit the list, but just add a single new item to it, you can try the following flow:

  • A GET "List" action that loads the whole current list from the database and displays it. This will be the initial page to display.
  • Have a GET "AddToList" action that renders a (partial) view with a <form> and all the inputs needed to add a single item to the list. This action can be called from the main page (e.g. using Html.RenderAction), and rendered inside the main page or a dialog. By separating this "create" usecase from the "display list" usecase, you will improve reusability and maintainability.
  • The POST "AddToList" action just adds the single item to the list in the database, then redirects to the GET "List" action to display the updated result.

With this flow, there is no need to POST all already existing list items, they can always be loaded from the backend/database if they need to be displayed. The advantage is that you will also see any items that have been added in the mean time by other users or processes. This should also take care of any sorting needed (new item will show up in the right position naturally).

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
0

If You don't want to use the database route, You can use ViewBag to store the list and then Json of System.Web.Helpers to handle conversions example

For the View you need

@{
    var RequestList = (List<string>)ViewBag.RequestList;
}

//you are using a null controller here --gotten from question
@using (Html.BeginForm("CreateComplex", null, FormMethod.Post, new { @class = "" }))
{
     <input type="hidden" name="RequestList" id="myHiddenInput" value="@Json.Encode(RequestList)" />       
     //other inputs that are binded to the model
}

The Controller

[HttpPost]
public ActionResult CreateComplex(Model model, string RequestList)
{
    var thelist = System.Web.Helpers.Json.Decode<List<string>>(RequestList);
    thelist.Add(model.TheAttrForAddition);
    //model.TheAttrForAddition is the attribute in the model the user is editing

    ViewBag.RequestList= thelist;
    //do other things and return the view
}
Bosco
  • 1,536
  • 2
  • 13
  • 25