简体   繁体   English

在MVC中将大数据从视图发送到控制器

[英]Sending Large data from View to Controller in MVC

I am getting table records, I need to send it to Controller so that I can send email.我正在获取表记录,我需要将其发送到 Controller 以便我可以发送电子邮件。 When I tried with following code, it's throwing error当我尝试使用以下代码时,它抛出错误

  var emailList = '';
  $('.checkBoxClass').each(function () {
    if ($(this).is(':checked')) {

        emailList += $(this).val() + ',';
    }
});

body = 'Hi Team'
console.log('emIl ' + emailList);
var baseUrl = ".";
$.ajax({
    type: "GET",
    url: baseUrl + "/Dashboard/GetFinanceSendMail",
    data:  "{'emailList': '" + JSON.stringify(emailList) +  "', body' : '" + body + "' }",
    success: function (json) {
        alert(json);
    }
});

Error as : HTTP Error 404.15 - Not Found The request filtering module is configured to deny a request where the query string is too long.错误为:HTTP 错误 404.15 - 未找到 请求过滤模块配置为拒绝查询字符串过长的请求。

Most likely causes: Request filtering is configured on the Web server to deny the request because the query string is too long.最可能的原因: 请求过滤在 Web 服务器上配置为拒绝请求,因为查询字符串太长。

I have tried to add following code, still same error我试图添加以下代码,仍然是同样的错误

var formData = new FormData();
var objArr = [];

objArr.push({ "emIl": emailList, });

//JSON obj
formData.append('objArr', JSON.stringify(objArr))


body = 'Hi Team'
console.log('emIl ' + emailList);
var baseUrl = ".";
$.ajax({
    type: "POST",
    url: baseUrl + "/Dashboard/GetFinanceSendMail",
    processData: false,
    contentType: false,
    data: formData,

Here is Controller Code这是控制器代码

    [HttpGet]
    public JsonResult GetFinanceSendMail(string emailList, string body)
    {
        List<string> emails = emailList.Split(',').ToList();
        // Send Email add optiona arg to the method
        _openPobl.TriggerFinanceEmail(body, emails);
        return Json(emails, JsonRequestBehavior.AllowGet);
    }

fix the action, remove [get]修复动作,删除 [get]

 
 Route[("~/Dashboard/GetFinanceSendMail")]
    public JsonResult GetFinanceSendMail(string emailList, string body)

and ajax和阿贾克斯

var emailList = '';
  $('.checkBoxClass').each(function () {
    if ($(this).is(':checked')) {

        emailList += $(this).val() + ',';
    }
});

var body = 'Hi Team';

$.ajax({
    type: "POST",
    url:  "/Dashboard/GetFinanceSendMail",
    data:  {emailList: emailList, body :  body  },
    success: function (json) {
        alert(json);
    }
});

but if you want to use post much more reliable to create a viewmodel class但是如果你想使用更可靠的 post 来创建一个视图模型类

public class ViewModel
{
public string EmailList {get;set;}
public string Body {get;set;}
}

action行动

   Route[("~/Dashboard/GetFinanceSendMail")]
   public JsonResult GetFinanceSendMail(ViewModel model)
.....

and ajax和阿贾克斯

$.ajax({
    type: "POST",
    url:  "/Dashboard/GetFinanceSendMail",
    data:  { model: {emailList: emailList, body :  body } },
    success: function (json) {
        alert(json);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM