How to change a model property from a javascript function inside a view?

末鹿安然 提交于 2021-01-27 07:11:32

问题



Is there any way to alter my Model properties from a javascript function inside a view?

Specifically, I have an edit view and need to access a string property value with

function SomeJSFunction() {
var somevar = '<%=Model.Property %>';
...

then do some changes on somevar and set the model property to the changed string. I'm doing a submit at this point, so it's not a question of dealing with the display, just need to alter the model from inside the function before I submit. I know I could pass the string as a parameter and deal with it inside the controller but it just doesn't cut it as I really need to be done with it in the view. Appreciate any help!


回答1:


You can't do this using javascript. The model is instantiated by the controller and passed to the view. If you want to modify some properties you will need to perform a request to a controller action and send the new values. This could be done either using a standard form or AJAX request.



来源:https://stackoverflow.com/questions/3912993/how-to-change-a-model-property-from-a-javascript-function-inside-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!