Home > Net >  Send List of IDs from List of Object From View to Controller C#
Send List of IDs from List of Object From View to Controller C#

Time:03-24

I have a view with a list of objects as its model

@model List<Users>

Inside that view, I have a form and button to submit the form in ASP.NET Core MVC:

<input  style=" width: 100px;" 
       type="submit" value="@localizer["Save"]" />

I need another button to cancel form submission and redirect to another method but I need to pass the list of Users with redirection at cancel button

I tried

<a asp-controller="User" asp-action="cancel" asp-route-ids="@Model.Select(x => x.id);">Cancel</a>

but it didn't work, the list is empty

CodePudding user response:

If you use asp-route-somekey to set the query, your target list name was recognized as value of the key "Ids",you could see the result as below: enter image description here

enter image description here

If you do want to pass list to the query,you could try:

<a asp-controller="User" asp-action="Cancel" asp-all-route-data="@(new Dictionary<string, string> { { "Ids[0]", "1" },{ "Ids[1]", "2" } })">Cancel</a>

The result: enter image description here

However the length of url is limited, it may cause some errors

  • Related