0

I'm writing an ASP.NET web app (university task for exam). I have a database which has columns like Id, Name, Age, SumNote. First of all I had to make a partial view with top 5 students in database:

This method to get top 5 students

public class HomeController : Controller
{
StudentContext db = new StudentContext();
        
public ActionResult ShowTopFive ()
{
var allStudents = db.Students.OrderByDescending(s => s.SumNote).Take(5);
                    return PartialView(allStudents);
}
}

This is the patrial View:

@model IEnumerable<Univercity.Models.Student>

<div id="results">
    <h4>Best 5 students</h4>
    <ul>
        @foreach (var item in Model)
        {
            <li>@item.Name, Summ of notes: @item.SumNote</li>
        }
    </ul>
</div>

and with this one I got the list of students in my webpage

<div>
    <h5>Show top 5 students</h5>
</div>
<div>
    @using (Ajax.BeginForm("ShowTopFive", new AjaxOptions { UpdateTargetId = "results" }))
    {
        <input type="submit" value="Show"/>
    }
    <div id="results"></div>
</div>

the output result looks like this:

Ivanov Mikhail, Summ of notes: 16

Kozlov Pete, Summ of notes: 12

Mary Ann, Summ of notes: 11

I also need to save it as text file. Can't figure out how? May be there is a way to change something in Ajax code?

Thanks in advance. Hope someone know how to do it. Google didn't help

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    You could add a link to another action method that does the top5 query, then writes the results to a text file. Return that new text file as the response. Like they do here: https://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc – L0uis Mar 18 '19 at 21:02
  • thanks for advice. Dunno how to write that as a method. My partial view use model student to get necessary data from database. How do I realize it in method? `var allStudents = db.Students.OrderByDescending(s => s.SumNote).Take(5);` returns only SQL request – Michael Blala Mar 18 '19 at 21:10
  • just did this: `var allStudents = db.Students.OrderByDescending(s => s.SumNote).Take(5);` Than I do `var dataStudents = allStudents.toList();` it will show me top5 students with all data like Age and other notes. I need to get only names and SumNote – Michael Blala Mar 18 '19 at 21:19
  • 1
    So iterate over each dataStudent, append the data you want from the collection in to a string, write that string to a file and return that file as the response. You have to write the list of objects out yourself in the new action method and put that result into a file. – L0uis Mar 18 '19 at 21:46

1 Answers1

1

You could create a controller action method which uses FileStreamResult by iterating the list created from ToList() and write necessary property values into a stream, then use Controller.File() overload which accepts stream to let user download text file:

public ActionResult GetTextFile()
{
    var topFiveStudents = db.Students.OrderByDescending(s => s.SumNote).Take(5).ToList();

    if (topFiveStudents != null && topFiveStudents.Count > 0) 
    {
        string fileName = "something.txt";

        // create a stream
        var ms = new MemoryStream();
        var sw = new StreamWriter(ms);

        foreach (var students in topFiveStudents)
        {
            // iterate the list and write to stream
            sw.WriteLine(string.Format("{0}, Sum of notes: {1}", students.Name, students.SumNote));
        }

        sw.Flush();
        ms.Position = 0;

        // return text file from stream
        return File(ms, "text/plain", fileName);
    }
    else
    {
        // do something else
    }
}

Afterwards, create an anchor link pointed to that action method mentioned above inside partial view:

@Html.ActionLink("Export to TXT", "GetTextFile", "ControllerName")
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61