English 中文(简体)
Invoke Action Solutions from Clicking a button in ASP. NET MVC
原标题:Invoke Action Method from Clicking a button in ASP.NET MVC

我想知道,如果点子被点击,我是否可以在控制器中采用某种方法。

我有以下看法:home,在装上该观点时,该观点在控制器中援引Ind。 我有<代码>Button(超文本或ASP.NET) ,称为<代码>LoadData。 当我点击 but子时,我需要把一些数据装上所谓的<代码>。 Home .

我如何这样做?

最佳回答

如果希望用美国宇宙航空研究开发机构的形式提供数据,就必须让JQuery或Java式读物公司参与。 但最简单的形式是:

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

 <!--Form values in here-->

 <input type="submit" />

<% Html.EndForm(); %>

将援引你的行动方法并援引背后。 该系统中有一个自动协调机制。 Web.Mvc.Ajax, 用AjaxForm和AJAX备选办法做背后yn,很容易安装。 我倾向于亲自使用JQuery。

HTH.

问题回答

为了利用一个简单员额来做到这一点,你刚刚需要把控制器方法存在的URL上调。

例如,遵循你全球确定的标准路线。

如果你在称为“LoadData”的家库勒班内有控制器方法,那么你可以使用URL:

/Home/Load 数据

这就是你加入以下表格的URL:action/code>属性。

你们也可以利用美国宇宙航空研究开发机构的请求,将数据输入你重新接下去的同一页,而没有任何回击。

你们可以做这样的事情:

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

您界定了两种行动,一种是表明空洞的观点,另一种是把观点列入清单:

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);
    }

以及您的看法

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

Empty

<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>

希望这一帮助

兹致函主计长如下。

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>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...