发送与jQuery数据到MVC控制器 [英] Send data with jquery to an MVC controller

查看:90
本文介绍了发送与jQuery数据到MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC3应用程序,当用户点击我的锚标记,我想送3个数据,一个动作:

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:

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

这是JavaScript调用我的行动:

This is the javascript to call my action:

   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)

该件即时关注的文件名和说明,因为这些可以loooooong,我不想URL显示,像这样:

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

 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

推荐答案

您可以使用jQuery的$。阿贾克斯方式:

You can use the jQuery $.ajax method:

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

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

这篇关于发送与jQuery数据到MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆