简体   繁体   中英

Asp.Net Sending large amounts of data from view to controller

I'm working on a Asp.net MVC application. In my project, I'm using a third party JavaScript library called Dhtmlx Scheduler .

There is a function in this library then writes all the data on the scheduler into XML format. I then need to manipulate this data, and write in back onto a <textarea> on my view page.

as of now this is what I have:

View:

function save() {
    var url = "/Home/Save"

    var xml = scheduler.toXML();

    $.ajax({
        url: url,
        Type: "POST",
        dataType: 'json',
        async: false,
        data: { xmlString: xml },
        contentType: 'application/json; charset=utf-8',
        success: alert("File Saved in C:\\ Drive as Tasks.xml")
    });
}

Controller:

public ActionResult Save(string xmlString)
{
    XmlDocument doc = new XmlDocument();
    try
    {
        doc.LoadXml(xmlString);
    }
    catch(Exception e)
    {
        Console.WriteLine(e);
    }
    doc.Save(@"C:\\Tasks.xml");

    W6ViewModel viewModel = new W6ViewModel();
    viewModel.engineers = db.W6ENGINEERS.ToList();
    viewModel.tasks = db.W6TASKS.ToList();
    viewModel.skills = db.W6TASKS_REQUIRED_SKILLS1.ToList();
    viewModel.categories = db.W6TASKTYPECATEGORY.ToList();

    gatherInfo(viewModel);

    return View("Index", viewModel);
}

When trying to save three events (Dhtmlx objects) it works flawlessly, when trying to add more data to the XML I get this error (read form FireBug):

The length of the query string for this request exceeds the configured maxQueryStringLength value.

Any help would be greatly appreciated. Thanks!

Firebug Console: 在此处输入图片说明

I believe jQuery.ajax uses type and not Type . This is case sensitive. If you look at your Firebug it is doing a GET request instead of a POST:

$.ajax({
    url: url,
    Type: "POST",
    dataType: 'json',
    async: false,
    data: { xmlString: xml },
    contentType: 'application/json; charset=utf-8',
    success: alert("File Saved in C:\\ Drive as Tasks.xml")
});

should be

$.ajax({
    url: url,
    type: "POST",
    dataType: 'json',
    async: false,
    data: { xmlString: xml },
    contentType: 'application/json; charset=utf-8',
    success: alert("File Saved in C:\\ Drive as Tasks.xml")
});

Grr I can't comment yet, I seem to recall if you pass one primitive value you do

data: xml 

rather than

data : { xmlString: xml }

has explained in this post -> request exceeds the configured maxQueryStringLength when using [Authorize]

you should change the configuration setting in your web.config to allow more that the 2048 character of the default value.

so basicly you need to change the <httpRuntime maxRequestLength = "" maxQueryStringLength =""> to the number you find acceptable.

Edit: have you tried to change your Type: "POST" to type: "POST" ? since javascript is a case sensitive language.

As for the invalid Json primitive try this :

    $.ajax({
            url: url,
            type: "POST",
            dataType: 'json',
            async: false,
            data: 'xmlString='+ xml,
            contentType: 'application/json; charset=utf-8',
            success: alert("File Saved in C:\\ Drive as Tasks.xml")
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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