I fill the input "txt" with values (several rows in the table).
I have checked in the javascript that the list contains several rows. And when I click on the Validation button the code goes to the controller but the parameter lstOption remains empty. Could you help me, please ?
Your Ajax call is incorrect. You are doing a form post, which is name / value pairs. The to string of lstOptions is not what you want. Using the browsers network debugger would have shown this. Try sending as json
public class TestAjaxController : Controller
{
// GET: TestAjax
public ActionResult Index()
{ //Here is the test data
var data = new List<OptionDTO>
{
new OptionDTO{ ID_OPTION="ID_OPTION1",LI_VALUE="LI_VALU1"},
new OptionDTO{ ID_OPTION="ID_OPTION2",LI_VALUE="LI_VALU2"},
new OptionDTO{ ID_OPTION="ID_OPTION3",LI_VALUE="LI_VALU3"},
new OptionDTO{ ID_OPTION="ID_OPTION4",LI_VALUE="LI_VALU4"}
};
return View(data);
}
[HttpPost]
public void CreateOption(List<OptionDTO> lstOption)
{
string dd = "";
}
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
3 Points
39 Posts
Cannot pass list of objects from view to controller via Ajax
Nov 24, 2020 01:57 PM|eric.bryan|LINK
Hello everybody,I cannot pass a list of objects to a controller via Ajax.
However my code is simple :
Here is the object class :
Here is my view :
</div> <div>
and here is the controller:
I fill the input "txt" with values (several rows in the table).
I have checked in the javascript that the list contains several rows.
And when I click on the Validation button the code goes to the controller but the parameter lstOption remains empty.
Could you help me, please ?
Thanks a lot in advance.
Eric.
All-Star
52151 Points
23251 Posts
Re: Cannot pass list of objects from view to controller via Ajax
Nov 24, 2020 03:10 PM|mgebhard|LINK
I built a test based on your code which works as expected. Compare your code to the the following.
All-Star
57864 Points
15493 Posts
Re: Cannot pass list of objects from view to controller via Ajax
Nov 24, 2020 03:34 PM|bruce (sqlwork.com)|LINK
Your Ajax call is incorrect. You are doing a form post, which is name / value pairs. The to string of lstOptions is not what you want. Using the browsers network debugger would have shown this. Try sending as json
Also you should not use eval().
Member
3 Points
39 Posts
Re: Cannot pass list of objects from view to controller via Ajax
Nov 24, 2020 08:46 PM|eric.bryan|LINK
mgebhard, bruce :
Thank you for your answers.
I will check them and will let you know.
Eric.
Contributor
2380 Points
683 Posts
Re: Cannot pass list of objects from view to controller via Ajax
Nov 25, 2020 05:26 AM|YihuiSun|LINK
Hi eric.bryan,
I have another solution, you can refer to it.
Controller
View
Here is the result.
Best Regards,
YihuiSun
Member
3 Points
39 Posts
Re: Cannot pass list of objects from view to controller via Ajax
Nov 25, 2020 09:53 AM|eric.bryan|LINK
Bruce, YihuiSun :
Thank you very much for your answers.
I have checked mgebhard answer and it works, it suits my needs.