通JS变量或INOUT值剃须刀 [英] Pass js variable or inout value to razor

查看:109
本文介绍了通JS变量或INOUT值剃须刀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过JS变量或HTML值剃刀?

Is it possible to pass js variable or html value to razor?

@{
    int n = ?????;
    Html.RenderPartial("_AddressBookEdit", @Model.Addresses[n]);
}

如何设置变量n从JavaScript或隐藏字段为它分配一个值?

How can I set variable n from javascript or assign it a value from a hidden field?

n = $('#myvar').val()'; 

以上code不起作用。

The above code does not work.

推荐答案

您在这里缺少一个基本的了解。我已经回答了你一个版本这个问题,但是我没想到你是误会了这一点。

You are missing a fundamental understanding here. I already answered a version of this question by you, however I did not think you misunderstood this point.

您不能从Javascript传递到剃刀无需任何通过服务器。

You cannot pass anything from javascript to razor without going through the server.

这是因为剃刀执行服务器侧(因此C#中的话)。该控制器被调用时,该类被实例化,那么该方法被调用它匹配请求的操作,然后返回一个视图。当返回查看,在查看C#code被执行。一旦所有code已被执行时,它发出的响应html页面,然后运行的JavaScript客户端上。供您javascript来剃刀搞定,你会发出一个新的请求要经过这条道路从一开始就为了回来了最后。

That is because the razor is executed server side (hence the c# in it). The controller is called, that class is instantiated, then the method is invoked which matches the action requested, and then a view is returned. When the view is returned, the c# code in the view is executed. Once all code has been executed, it issues the html page in the response and then the javascript runs on the client. For you to get from javascript to razor, you would have to issue a new request to go through that path from the beginning in order to come back out in the end.

解决这个问题的服务器端。

Solve this issue server side.

这篇关于通JS变量或INOUT值剃须刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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