简体   繁体   English

如何从MVC3中的PartialView调用控制器

[英]How to call a Controller from a PartialView in MVC3

I have a PartialView which I need to create a button on, and once a user clicks on this button, it must send a HTTPGET to a controller that receives the model. 我有一个PartialView,需要在其上创建一个按钮,一旦用户单击该按钮,它就必须将HTTPGET发送到接收模型的控制器。 How do I call the HTTPGET action from the PartialView? 如何从PartialView调用HTTPGET操作?

Any idea as to how to do this in MVC3? 关于如何在MVC3中执行此操作的任何想法?

There are a few ways that you can achieve this. 有几种方法可以实现此目的。 The easiest way is to use an ajax request to send the data back to the controller using jQuery (http://api.jquery.com/jQuery.get/). 最简单的方法是使用ajax请求,使用jQuery(http://api.jquery.com/jQuery.get/)将数据发送回控制器。 However I would not use a HTTP GET for this. 但是,我不会为此使用HTTP GET。 Although not enforced it is best to stick to using the HTTP verbs in the way that they were intended ie GET is for receiving data, POST is for sending data to the server. 尽管未强制执行,但最好还是按照预期的方式使用HTTP动词,即GET用于接收数据,而POST用于将数据发送到服务器。

may be this solution work : 可能是此解决方案的工作:

<form method="get" action="controllerName" enctype="multipart/form-data">
 @html.partial("viewName")
 <input type="submit" value="Send" ... />
</form>

use this to create the form in your partial view, 使用它在您的局部视图中创建表单,

@{using (Html.BeginForm("Create", "Person", FormMethod.Get, new { 
                                                   enctype = "multipart/form-data", 
                                                   id = "<id of the form>" }))
{

//body of your form

}

Here you can see that, type of the form method has been passed in to the Html.BeginForm method as "FormMethod.Get". 在这里您可以看到,表单方法的类型已作为“ FormMethod.Get”传递给Html.BeginForm方法。 If you want to sent the response to a Post method, uset "FormMethod.Post". 如果要将响应发送到Post方法,请使用“ FormMethod.Post”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM