C# 使用 jquery 将数据发送到 MVC 控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11016388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:08:01  来源:igfitidea点击:

Send data with jquery to an MVC controller

javascriptc#jqueryasp.netasp.net-mvc

提问by BoundForGlory

I have an ASP.NET MVC3 app and when the user clicks on my anchor tag, I want to send 3 pieces of data to an action:

我有一个 ASP.NET MVC3 应用程序,当用户单击我的锚标记时,我想向操作发送 3 条数据:

 <a onclick='editDescription(<#= DocID,FileName,Description #>)'></a>

This is the javascript to call my action:

这是调用我的操作的 javascript:

   function editDescription(docId,fileName,description) {
     var url = "@Url.Content("~/OrderDetail/_EditDescription/")" + docId+'/'+
     fileName + '/' + description;
    //do the rest}

My action:

我的行动:

  public ActionResult _EditDescription(string id,string filename, string descritpion)

The pieces im concerned about are FileName and Description because these can be loooooong and i dont want a url to appear like so:

我关心的部分是 FileName 和 Description,因为它们可能是 loooooong,我不希望 url 看起来像这样:

 http://localhost/OrderDetail/_EditDescription/123/some long filename.pdf/this is a    long description for the name

How can i send across my data to my action without having to send it like a query string? Thanks

如何将我的数据发送到我的操作而不必像查询字符串一样发送它?谢谢

采纳答案by David East

You can use the jQuery $.ajax method:

您可以使用 jQuery $.ajax 方法:

<div id="what-I-want-updated">

  <input id="whatever-the-id-is" type="text" value="@Model.ID" />
  <br /> 
  <input id="whatever-the-filename" type="text" value="@Model.Filename" />
  <br />
  <input id="whatever-the-description" type="text" value="@Model.Description" />
  <br />
  <button id="whatIsClicked">Update!</button>

</div> <!-- /#what-I-want-updated -->

<script>

    // You're probably clicking something to initiate update 
    var $whatIsClicked = $('#whatIsClicked');

    // .live persists on the page even after other ajax calls
    // So when the thing is clicked
    $whatIsClicked.live('click', function() {

       // Grab the information needed to update
       var theId = $('#whatever-the-id-is').val(); //Or it could be .text()
       var theFilename = $('#whatever-the-filename').val();
       var theDescript = $('#whatever-the-description').val();

       // Let's edit the description!
       $.ajax({
         type: "POST",
         url: "OrderDetail/_EditDescription", // the method we are calling
         contentType: "application/json; charset=utf-8",
         data: {id: theId, filename: theFilename, description: theDescript},
         dataType: "json",
         success: function (result) {
             alert('Yay! It worked!');
             // Or if you are returning something
             alert('I returned... ' + result.WhateverIsReturning);                    
         },
         error: function (result) {
             alert('Oh no :(');
         }
     });
    });
</script>

Even though it will still work, make sure you change your Controller method to:

即使它仍然有效,请确保将 Controller 方法更改为:

[HttpPost]
public ActionResult _EditDescription(string id, string filename, string descritpion)

回答by AD.Net

You can do a full post of the form if you like either through ajax $.postor by having an action with [HttpPost]attribute.

如果您喜欢通过 ajax$.post或通过具有[HttpPost]属性的操作,您可以完成表单的完整发布。

回答by Terry

Declare your action as a POST

将您的操作声明为POST

[HttpPost]
public ActionResult _EditDescription(string docId, string filename, string description)

Create an invisible HTML form:

创建一个不可见的 HTML 表单:

<form action="@Url.Content("~/OrderDetail/_EditDescription/")" method="post" name="editDescriptionForm">
   <input type="hidden" name="docId" />
   <input type="hidden" name="fileName" />
   <input type="hidden" name="description" />
</form>

Fill out the form and submit it with JS:

填写表单并用JS提交:

function editDescription(docId, fileName, description) {
    document.editDescriptionForm.docId = docId;
    ...

    document.editDescriptionForm.submit();
}