Click here to Skip to main content
15,886,860 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a javascript array and i want to assign all its value to MVC Model array.

For ex.

var selectedValues = [];
$.each($('#ImageExtensionList option:selected'), function (index, value) {
selectedValues.push(value);

here, @Model.OptionList = selectedValues i want to assign but not working.

Can anyone share on this?

Thanks.
Posted
Comments
F-ES Sitecore 3-Dec-15 11:18am    
You can't assign anything in MVC via javascript as MVC code executes on the server and the resultant html and javascript runs on the client. Your server code isn't "running inside" the browser.

To pass things to the model you need to put the data in a form element that then binds to your model property via model binding when the form is submitted. If you google for dealing with mvc arrays and javascript you'll see examples of some techniques that can bind arrays to models. Alternatively you might be better just using ajax to post the array direct to an action; it's hard to tell from your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900