【问题标题】:Invoke Action Method from Clicking a button in ASP.NET MVC通过单击 ASP.NET MVC 中的按钮调用操作方法
【发布时间】:2009-12-22 10:22:44
【问题描述】:

我想知道单击按钮时是否可以调用控制器中的方法。

我有一个名为home 的视图,当加载视图时,它会调用控制器中的Index 操作方法。我有一个名为 LoadDataButton(HTML 或 ASP.NET)。当我单击按钮时,我需要在名为Home 的同一视图中加载一些数据。

我该怎么做?

【问题讨论】:

  • @engineerachu 我编辑了不正确的术语。对于 ASP.NET MVC,请勿使用 ASP.NET 控件按钮或其他方式。这不是 ASP.NET——Home.aspx 不是一个页面——它是一个视图。它实际上并不对应于物理页面。这是一个重要的区别。

标签: c# .net asp.net-mvc button controller


【解决方案1】:

如果您想以 AJAX 形式进行,则必须使用 JQuery 或 JavaScript 来调用以从服务器获取数据。但以最简单的形式,这样做:

<% Html.BeginForm("Action", "Controller"); %>

 <!--Form values in here-->

 <input type="submit" />

<% Html.EndForm(); %>

将调用您的操作方法并调用回发。 System.Web.Mvc.Ajax 中有一个 AJAX 选项,可以使用带有 AJAX 选项的 AjaxForm 来执行回发异步,而且很容易设置。我个人倾向于使用 JQuery。

HTH。

【讨论】:

    【解决方案2】:

    要使用简单的发布来做到这一点,您只需发布到控制器方法所在的 URL。

    例如,遵循 global.asax "{controller}/{action}/{id}" 中的标准路由设置

    如果您的 HomeController 类中有一个名为“LoadData”的控制器方法,那么您可以通过 URL 访问它:

    /Home/加载数据

    这是您在表单action 属性中输入的网址。

    您还可以使用 AJAX 请求点击此 URL,以便将数据加载到您所在的同一页面中,而无需任何回发。

    使用 jQuery,您可以执行以下操作:

    $('#myForm').submit(function() {
                       $.post(
                                this.action,{params},
                                function(data){ // do something with return info }
                      }
    

    【讨论】:

    • 是的,我知道我可以使用 jQuery 做到这一点,但我首先尝试使用 asp.net,然后才转向 jQuery。
    【解决方案3】:

    您定义了两个操作,一个用于显示空视图,另一个用于使用列表填充视图:

    public ViewResult Empty()
        {
            var products = productsRepository.Products.Where(x => x.ProductID == -1);
            return View();
        }
    

    和:

    public ViewResult ListAll()
        {
            var products = productsRepository.Products;
            return View("Empty", products);
        }
    

    在你看来 Empty.aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<DomainModel.Entities.Product>>" %>
    

    空的

    <h2>Empty</h2>
    
    <table>
        <tr>
            <th></th>
            <th>
                ProductID
            </th>
            <th>
                Name
            </th>
            <th>
                Description
            </th>
            <th>
                Price
            </th>
            <th>
                Category
            </th>
            <th>
                ImageMimeType
            </th>
            <th>
                Error
            </th>
        </tr>
    
    <% if (Model != null) 
       {
           foreach (var item in Model)
           { %>
    
        <tr>
            <td>
                <%= Html.ActionLink("Edit", "Edit", new { id = item.ProductID })%> |
                <%= Html.ActionLink("Details", "Details", new { id = item.ProductID })%>
            </td>
            <td>
                <%= Html.Encode(item.ProductID)%>
            </td>
            <td>
                <%= Html.Encode(item.Name)%>
            </td>
            <td>
                <%= Html.Encode(item.Description)%>
            </td>
            <td>
                <%= Html.Encode(String.Format("{0:F}", item.Price))%>
            </td>
            <td>
                <%= Html.Encode(item.Category)%>
            </td>
            <td>
                <%= Html.Encode(item.ImageMimeType)%>
            </td>
            <td>
                <%= Html.Encode(item.Error)%>
            </td>
        </tr>
    
    <% }
       }%>
    
    </table>
    
    <p>
        <%= Html.ActionLink("List Products", "ListAll")%>
    
    </p>
    

    希望对你有帮助

    【讨论】:

    • 这是如何工作的?就像单击“ListAll”链接时,“ListAll”方法会触发还是什么? “ViewResult”类是什么意思?
    • 是的,当单击 ListAll 链接时,将调用 ListAll 方法。有一些结果,操作方法可能返回其中之一是 View,因此 ViewResult 在此处返回一个视图 Empty.aspx 视图,其中产品列表为 ViewModel。因为它表明你对 MVC 很陌生,所以我建议你看看 asp.net/MVC 你会在那里找到很棒的视频和教程,你可以在 ASP.NET MVC 论坛 (forums.asp.net/1146.aspx) 上提问以获得正确的答案如果我的不够好。希望这会有所帮助。
    【解决方案4】:

    您必须按如下方式编写控制器。

    public class HomeController : Controller
    {
    
        public ActionResult Index()
        {
            return View();
        }
    
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult FetchData()
        {
            return Content("Some data...");
        }
    
    }
    

    并按如下方式调用所需的操作。

    <% using (Ajax.BeginForm("FetchData", new AjaxOptions() { UpdateTargetId = "ContentPlaceHolder", HttpMethod = "Post" }))
       { %>
    <div id="ContentPlaceHolder"></div>
    <input type="submit" value="Fetch Data" />
    <% } %>
    
    <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
    

    【讨论】:

      猜你喜欢
      • 2020-01-10
      • 1970-01-01
      • 2018-08-24
      • 2019-02-28
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多