c# asp.net: add element to a list in a model using form

痴心易碎 提交于 2020-01-06 07:17:14

问题


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


回答1:


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).




回答2:


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
}


来源:https://stackoverflow.com/questions/57003054/c-sharp-asp-net-add-element-to-a-list-in-a-model-using-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!