如何从视图内的 javascript 函数更改模型属性? [英] How to change a model property from a javascript function inside a view?

查看:63
本文介绍了如何从视图内的 javascript 函数更改模型属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


有没有办法从视图内的 javascript 函数更改我的模型属性?


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 %>';
...

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

然后对 somevar 进行一些更改并将模型属性设置为更改后的字符串.我此时正在做一个提交,所以这不是处理显示的问题,只需要在我提交之前从函数内部更改模型.我知道我可以将字符串作为参数传递并在控制器内处理它,但它只是没有削减它,因为我真的需要在视图中完成它.感谢您的帮助!

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!

推荐答案

您无法使用 javascript 执行此操作.模型由控制器实例化并传递给视图.如果要修改某些属性,则需要对控制器操作执行请求并发送新值.这可以使用标准表单或 AJAX 请求来完成.

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.

这篇关于如何从视图内的 javascript 函数更改模型属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆