Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8072887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:10:08+00:00 2026-06-05T14:10:08+00:00

I’ve passed my model in the View to the Controller with the following jQuery

  • 0

I’ve passed my model in the View to the Controller with the following jQuery Ajax code:

$.ajax({
        data: model,
        type: "POST",
        url: '@Url.Action("createDoc")',
        datatype: "json",
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            alert('Done '+ result.toString());
        }
    });

The problem is: in the Controller method “createDoc”

[HttpPost]
    public ActionResult createDoc(IEnumerable<Movie> movies)
    {
        CreateWordprocessingDocument(movies);
        return Json(new { result = movies.Count()});
    }

I can’t do anything with the movies Enumerable data.
The call to the CreateWordProcessingDocument create a document with the movies data. But it does not.
This is the method code:

public void CreateWordprocessingDocument(IEnumerable<Movie> movies)
{
    HttpContextWrapper context = new HttpContextWrapper(System.Web.HttpContext.Current);
    context.Response.Clear();
    context.Response.Buffer = true;
    context.Response.AddHeader("content-disposition", "attachment;filename=example.doc");
    context.Response.ContentType = "application/vnd.ms-word.document";
    context.Response.Charset = "";
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("<p  align='Center'><b> GENERAL TITLE</b></p>");
    sb.Append("<br>"+movies.Count());
    for (int i = 0; i < movies.Count(); i++) {
        sb.Append("<br>Title:" + movies.ElementAt(i).Title +"");
    }
    context.Response.Output.Write(sb.ToString());
    context.Response.Flush();
    context.Response.End();
}

But it doesn’t work: it returns to the ajax post and pop ups the “Done” alert, with the result.tostring() showing the HTML code I create on the createWordProcessingDocument method.
How can I avoid this behaviour so I can do something with the data I pass to the controller from the View?
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:10:09+00:00Added an answer on June 5, 2026 at 2:10 pm

    Problem solved: I post the solution here, if it can helps someone with my problem.
    In my createDoc method I create the string with the wanted data, then I put them in a tempData proprierty. That’s what createDoc looks like:

    [HttpPost]
        public void createDoc(IEnumerable<Movie> movies)
        {
            String mov="";
            mov = mov + "<br>" + movies.Count();
            for (int i = 0; i < movies.Count(); i++)
            {
               mov=mov+"<br>Title:" + movies.ElementAt(i).Title + "";
            }
            TempData["movies"] = mov;
        }
    

    Then the “control” returns to the Ajax post method, and at the end of it I put this:

    success: function (result) {
                window.location.href = '@Url.Action("CreateWordprocessingDocument","Movies")'
            }
    

    So my Ajax post full code is:

    <script type="text/javascript">
    function postData() {
        var urlact = '@Url.Action("createDoc")';
        var model = '@Html.Raw(Json.Encode(Model))';
    
        alert(model);
        alert(JSON.stringify(model));
    
        $.ajax({
            data: model,
            type: "POST",
            url: urlact,
            datatype: "json",
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                window.location.href = '@Url.Action("CreateWordprocessingDocument","Movies")'
            }
        });
    }
    </script>
    

    Now the “control” is redirect to the CreateWordProcessingDocument action, where I actually create the document:

    public void CreateWordprocessingDocument()
        {
            string movies = TempData["movies"] as string;
            context.Response.Clear();
            context.Response.Buffer = true;
            context.Response.AddHeader("content-disposition", "attachment;filename=example.doc");
            context.Response.ContentType = "application/vnd.ms-word.document";
            context.Response.Charset = "";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<p  align='Center'><b>TITLE</b></p>");
            sb.Append(movies);
            context.Response.Output.Write(sb.ToString());
            context.Response.Flush();
            context.Response.End();
        }
    

    That’s all. Thank u all for your help, hope that I can help someone with this solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.