0
//vm    
class myViewModel {
    public Ilist<GenericClass> MyList {get;set;}
    }

//controller
[HttpPost]
public ActionResult MyAction(IList<GenericClass> myList){
// do something with list
}

// view
ım trying solution like this 
@model myViewModel 
....

<script>
var list = (@Model.MyList)
 $.post("@Url.Action("MyAction", "MyController")", { myList: list }, function (d) {
                console.log(d);
            });
</script>

How can ı pass generic list from view to my action. My list in my view model and ı want use it from view model like @Model.myList.

2
  • Do you only want to use jQuery, as per the post title? Or will you consider other solutions? If you create the list as a collection of hidden fields on the page, they will be accessible in the controller Post method. Commented Dec 7, 2021 at 14:01
  • yes i just want to use jQuery. Makes sense but that's not the solution I'm looking for
    – hamdi
    Commented Dec 7, 2021 at 14:58

1 Answer 1

0

you can use AJAX call like this

var list = JSON.stringify({ 'list': @Model.MyList });

$.ajax({
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    type: 'POST',
    url: '/MyController/MyAction',
    data: list,
    success: function () {          
        alert('List has been sent successfully.');
    },
    error: function (response) {          
        alert('Something gone bad, sorry bro');
    }
}); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.