Click here to Skip to main content
15,067,832 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one model, in that model one list object. Now, I want to assign value into list object using javascript in view. So, how can I assign value to model object in javascript?

What I have tried:

function updatePostID(val)
{
@Model.ListPostID = val;
}
Posted
Updated 18-May-16 1:24am
v2
Comments
Prateek Dalbehera 18-May-16 7:51am
   
Sorry bro, you cant assign value to a server side variable(here,Model(ListPostID)). Because, when you run the application, the server side code will not appear there.

You have send this data to server side through ajax or any other method and update the same.
F-ES Sitecore 18-May-16 8:38am
   
You can't do this as the model is an object in the memory on the server and only exists while the html to send to the client is being generated. Once the client has the html that is all they have, your server code isn't "running inside" the browser or on the page.

Take some time to actually understand the asp.net lifecycle and how the client\server model works and you'll understand why you can't do these things.

If you want data sent to the server you'll have to use ajax, or put the data in hidden fields in a form which is later submitted.

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