简体   繁体   English

如何从视图中的javascript函数更改模型属性?

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


Is there any way to alter my Model properties from a javascript function inside a view? 有没有办法从视图中的javascript函数改变我的模型属性?

Specifically, I have an edit view and need to access a string property value with 具体来说,我有一个编辑视图,需要访问字符串属性值

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

then do some changes on somevar and set the model property to the changed string. 然后对somevar进行一些更改并将model属性设置为更改的字符串。 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! 感谢任何帮助!

You can't do this using javascript. 你不能用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. 这可以使用标准表单或AJAX请求来完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 model 属性从 Razor 查看到 JavaScript ZC1C425268E68385D1AB5074C17A - How to pass model property from Razor View to JavaScript function 如何在视图中将模型变量设置为$(“ form”)。submit函数内部javascript中的值 - How to set a model variable in a view to a value from within javascript inside a $(“form”).submit function 动态更改javascript过滤器函数内部的属性 - Dynamically change property inside of javascript filter function JavaScript:如何从原型属性上的另一个函数内部调用原型属性上的函数,都在闭包内部? - JavaScript: how to call a function on the prototype property, from inside another function on the prototype property, all inside a closure? 如何在javascript中从函数内部访问对象属性,是否可能? - how to access object property from inside function in javascript, is it possible? 如何将多个参数从javascript函数传递到视图内的控制器? - How to pass multiple parameter from javascript function to controller inside a View? 如何在另一个视图模型内的视图模型中调用函数? - How to call a function in a View Model inside another View Model? 在razor视图中,如何将模型属性传递给JavaScript函数并显示返回值? - In razor view, how to pass model property to JavaScript function and display return value? 如何将模型字段传递给视图中的Javascript函数? - How to pass a model field to a Javascript function in a view? 如何在DOM中更改属性并将其保存回模型以在单独的视图中使用? - How to change property in DOM and save back to model for use in separate view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM