如何根据ASP.NET MVC中的控制器操作在我的页面javascript中设置变量?

时间:2021-07-31 03:43:31

So, I have a page that looks like the following: alt text http://img704.imageshack.us/img704/5973/croppercapture3.png

所以,我有一个如下所示的页面:alt text http://img704.imageshack.us/img704/5973/croppercapture3.png

This is my basic messaging inbox. I have 2 pages right now, Inbox and Sent (there will eventually be others). These both share the same View, and just have 2 different actions to populate the appropriate data.

这是我的基本消息收件箱。我现在有2页,收件箱和已发送(最终会有其他页面)。它们共享相同的View,并且只有2个不同的操作来填充适当的数据。

The elements on the right-hand side (the message list) is a partial view which has its data populated based on my two controller actions Inbox(int? page) and Sent(int? Page).

右侧的元素(消息列表)是一个局部视图,其中的数据基于我的两个控制器操作Inbox(int?page)和Sent(int?Page)填充。

So, I have one view "MessageView" and one partial view "MessageList" shared between Inbox/Sent.

所以,我有一个视图“MessageView”和一个部分视图“MessageList”在收件箱/已发送之间共享。

However, I now have to get those arrow buttons ("<" ">") working using Ajax. I know how to use jQueries ajax calls well enough, and I know how to render the result of the action call (which returns a partial view).

但是,我现在必须使用Ajax使用那些箭头按钮(“<”“>”)。我知道如何使用jQuery的ajax调用,我知道如何渲染动作调用的结果(返回局部视图)。

The problem comes from the fact that the javascript that makes these pagination ajax calls needs to know two things:

问题来自于使这些分页ajax调用的javascript需要知道两件事:

  • What the current page is (whether it be /messages/inbox or /messages/sent)
  • 当前页面是什么(无论是/ messages / inbox还是/ messages / sent)

  • What the current page is (specified in the query string, ie /messages/inbox?page=2).
  • 当前页面是什么(在查询字符串中指定,即/ messages / inbox?page = 2)。

Without knowing which page I'm on (Inbox or Sent), it wont know which url to make the ajax call on. Should it make the postback to /messages/inbox or to /messages/sent?

在不知道我在哪个页面(收件箱或已发送)的情况下,它不知道在哪个URL上进行ajax调用。它应该回发到/ messages / inbox还是/ messages / sent?

If I wasn't making these messages load with Ajax it would be as simple as loading the appropriate url into the link tags for the "<" and the ">" buttons. But I can't, because part of my requirements states that it must load the messages below without visibly refreshing to a new page.

如果我没有使用Ajax加载这些消息,那么就可以将相应的url加载到“<”和“>”按钮的链接标记中。但我不能,因为我的部分要求声明它必须加载以下消息而不会明显刷新到新页面。

2 个解决方案

#1


1  

In JavaScript you can check window.location.pathname to see the pathname section of the current’s page’s URL.

在JavaScript中,您可以检查window.location.pathname以查看当前页面URL的路径名部分。

window.location.search gives you the query string.

window.location.search为您提供查询字符串。

#2


0  

When the user clicks the Inbox or Sent buttons, you need to rewrite the URLs in your arrows so that they point to the right place.

当用户单击“收件箱”或“已发送”按钮时,您需要重写箭头中的URL,以使它们指向正确的位置。

#1


1  

In JavaScript you can check window.location.pathname to see the pathname section of the current’s page’s URL.

在JavaScript中,您可以检查window.location.pathname以查看当前页面URL的路径名部分。

window.location.search gives you the query string.

window.location.search为您提供查询字符串。

#2


0  

When the user clicks the Inbox or Sent buttons, you need to rewrite the URLs in your arrows so that they point to the right place.

当用户单击“收件箱”或“已发送”按钮时,您需要重写箭头中的URL,以使它们指向正确的位置。