How to Send List of Objects to View and Back to Post Method in Controller

How to send list of Objects to View and back to Post method in controller

I think this link will help you set up what you are trying to do:

http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/

It looks like in your form you need to modify it to something like:

<form:form method="post" action="savePerson" modelAttribute="persons">
<c:forEach var="person" items="${persons}" varStatus="status">
<form:input path="person[${status.index}].FName" name="FName" id="FName" value="" />
<form:input path="person[${status.index}].LName" name="LName" id="LName" value="" />
</c:forEach>

This SO question has a pretty good example that might help you out too: List<Foo> as form backing object using spring 3 mvc, correct syntax?

How to post listmodel object back to controller action method?

I had to take advantage of asp.net MVC concepts like, temp data, view data, to pass the data from one controller action to another. The whole idea was not to use javascript libraries and bootstrapping, so used temp data and view data to achieve this.
Thanks for all the support, community.

how to pass list of object from view to controller in asp.net mvc

You can't use @foreach loop in the view in order to pass list of object back to the controller, because that would generate input elements with same IDs and Names.

Instead, use standard for loop:

for(int i = 0; i < Model.Count; i++)
{
<div class="card-body">
<div class="form-group">
@Html.HiddenFor(m => m[i].Id)
<label for="exampleInputPassword1">@Model[i].Name</label>
@Html.EditorFor(m => m[i].Value,
new { htmlAttributes = new { @class = "form-control"} })
@Html.ValidationMessageFor(m => m[i].Value,
new { @class = "text-danger" })
</div>
</div>
}

Spring - Send List of Objects to View and back to Post only one Object in controller

  1. You should handle edit and delete in separate controller methods.

  2. Change your submit buttons to input type="button" and handle on click js event where you can build the logic to decide which controller method has to be called(may be can build the url dynamically) and submit it from js.

  3. After the operation of edit/delete you may need to call the get which in turn queries the db and user can see the latest results on your jsp.

Pass a list from get method (or from a view) to post method ASP .NET MVC

First, instead of binding the view to a list of students, bind the model to your Class object that has a list of students in it. That way, when you submit (post), you are sending the whole Model object into the Controller.

Change your AddStudentsPost method to take in Class object and do your logic there.

Finally, change the name of the "Class" object to something else, such as, "Course". Class should be reserved for actual classes so not to cause confusion.

You aren't far off, so keep going

How to pass List model to controller from view http post

Items in CategoryModel should be a property not a filed,so your CategoryModel should be like below:

public class CategoryModel
{
public string Label { get; set; }
public bool Active { get; set; }
public decimal Amount { get; set; }
public string Frequency { get; set; }
public string Type { get; set; }
public List<ItemModel> Items { get; set; }
}


Related Topics



Leave a reply



Submit