如何在没有服务的情况下将mjc4与ajax一起使用?

时间:2022-12-04 08:02:06

I'm pretty new to MVC and I'm just thinking ahead to my next project. I haven't been able to find any examples of how to do this, so I apologize if it seems like a question that may have already been answered somewhere else.

我是MVC的新手,我只是在思考我的下一个项目。我无法找到任何如何做到这一点的例子,所以如果它似乎是一个可能已在其他地方得到解答的问题,我道歉。

I'm going to want to create asynchronous cascading dropdown menus. So menu B will update as the values in menu A change, and menu C will update as either A or B update. What is the normal pattern for this?

我想要创建异步级联下拉菜单。因此菜单B将在菜单A中的值更改时更新,菜单C将更新为A或B更新。这是什么样的正常模式?

Normally, as list events fire, I would use ajax to call a service that would process my input data and return json which I would use to update my lists. In this case, there is no service, only the controller feeding the view. We could create a service, but we prefer not to if at all possible. So I'm wondering if there is a technique in MVC4 that can feed me back what I need to update the view without re-rendering the page.

通常,当列表事件触发时,我会使用ajax来调用一个服务来处理我的输入数据并返回json,我会用它来更新我的列表。在这种情况下,没有服务,只有控制器提供视图。我们可以创建一个服务,但我们不愿意,如果可能的话。所以我想知道MVC4中是否有一种技术可以反馈我需要更新视图而无需重新渲染页面。

If someone could refer me to an example or at least explain what I might be looking for, that would be a great help. TIA

如果有人可以将我推荐给一个例子或者至少解释一下我可能正在寻找什么,那将是一个很大的帮助。 TIA

2 个解决方案

#1


1  

You can implement the Ajax callback URL as a controller action (in fact, that is how I typically do it).

您可以将Ajax回调URL实现为控制器操作(事实上,这就是我通常这样做的方式)。

There is no need for a separate service.

不需要单独的服务。

public class CallbackController : Controller
{
    public ActionResult MenuOptions()
    {
        // return e.g. JSON 
    }
}

The Ajax URL would be

Ajax URL将是

http://myserver.com/Callback/MenuOptions

http://myserver.com/Callback/MenuOptions

#2


1  

There are a lot of answers around the web for this type of functionality. Here is one, and here is another similar example. What you should really do is read up on MVC actions / controllers and do some spiking / messing around.

网络上有很多关于此类功能的答案。这是一个,这是另一个类似的例子。你应该做的是读取MVC动作/控制器并做一些尖峰/乱码。

Building the example starter app will help you get a grasp on the differences between classic ASP.NET and MVC.

构建示例入门应用程序将帮助您掌握经典ASP.NET和MVC之间的差异。

#1


1  

You can implement the Ajax callback URL as a controller action (in fact, that is how I typically do it).

您可以将Ajax回调URL实现为控制器操作(事实上,这就是我通常这样做的方式)。

There is no need for a separate service.

不需要单独的服务。

public class CallbackController : Controller
{
    public ActionResult MenuOptions()
    {
        // return e.g. JSON 
    }
}

The Ajax URL would be

Ajax URL将是

http://myserver.com/Callback/MenuOptions

http://myserver.com/Callback/MenuOptions

#2


1  

There are a lot of answers around the web for this type of functionality. Here is one, and here is another similar example. What you should really do is read up on MVC actions / controllers and do some spiking / messing around.

网络上有很多关于此类功能的答案。这是一个,这是另一个类似的例子。你应该做的是读取MVC动作/控制器并做一些尖峰/乱码。

Building the example starter app will help you get a grasp on the differences between classic ASP.NET and MVC.

构建示例入门应用程序将帮助您掌握经典ASP.NET和MVC之间的差异。