admin管理员组文章数量:1323736
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!
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!
Share Improve this question asked Oct 12, 2010 at 8:59 pklosinskipklosinski 2293 silver badges9 bronze badges 2- i don't think you can change what is already in the ViewModel - i believe this is read-only. Why don't you set a hidden field in the form (from javascript), then you can look at that in the Request.Form collection when you submit? – RPM1984 Commented Oct 12, 2010 at 9:04
- yes, it is read-only. think of the model as being a 'served' response. once it's written to the page, the response is over. it's not like the bound controls in webfroms that talk to the page life-cycle. you'll have to 'post' the changes back via either a form or via jquery ajax etc.. this is basically how mvc works, so there's plenty of stuff out there to reference. – jim tollan Commented Oct 12, 2010 at 9:11
1 Answer
Reset to default 7You 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.
本文标签: aspnet mvcHow to change a model property from a javascript function inside a viewStack Overflow
版权声明:本文标题:asp.net mvc - How to change a model property from a javascript function inside a view? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742119445a2421627.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论