最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass js variable or inout value to razor - Stack Overflow

programmeradmin6浏览0评论

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

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

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

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

The above code does not work.

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

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

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

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

The above code does not work.

Share Improve this question edited Jul 7, 2015 at 22:56 abatishchev 100k88 gold badges301 silver badges442 bronze badges asked Apr 13, 2013 at 18:10 pts4pts4 4991 gold badge5 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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.

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

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 e back out in the end.

Solve this issue server side.

发布评论

评论列表(0)

  1. 暂无评论