【问题标题】:Send data with jquery to an MVC controller使用 jquery 将数据发送到 MVC 控制器
【发布时间】:2012-06-16 11:59:47
【问题描述】:

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

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

这是调用我的操作的 javascript:

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

我的行动:

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

我关心的部分是文件名和描述,因为它们可能很长,我不希望 url 看起来像这样:

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

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

【问题讨论】:

  • 您是否尝试过使用 type:'POST' 制作 $.ajax?
  • 不...你能提供一个快速的样本吗?
  • @David 已经做到了 :),见下文。

标签: javascript c# jquery asp.net asp.net-mvc


【解决方案1】:

您可以使用 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>

即使它仍然可以工作,请确保将控制器方法更改为:

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

【讨论】:

    【解决方案2】:

    将您的操作声明为 POST

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

    创建一个不可见的 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>
    

    填写表格并用JS提交:

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

    【讨论】:

      【解决方案3】:

      如果您愿意,可以通过 ajax $.post 或通过 [HttpPost] 属性执行操作来完成表单的完整发布。

      【讨论】:

      • 这不是表单加上视图设置的方式,用户可以独立于其他人做多项事情,因此不需要表单。我需要您建议的第二件事的快速示例
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      相关资源
      最近更新 更多