logo
down
shadow

return a View and File in a single method in ASP.Net MVC


return a View and File in a single method in ASP.Net MVC

By : user2956455
Date : November 22 2020, 10:56 AM
hope this fix your issue I am building an app in ASP.Net MVC, and want to return a View AND offer a download to the user. Is it possible? Right now, I can return a new page using , Please use like below.
code :
ViewData["text"] = "text that you need to return";
ViewData["FileName"] = "Name of the file that you need to return";
ViewData["Filepath"] = "Path of the file that you need to return";

return View();
@{
    var text = ViewData["text"];
    var filename = ViewData["FileName"];
    var filePath = ViewData["Filepath"];
}
public class FileDetails
{
    public string Text { get; set; }

    public string FileName { get; set; }

    public string Filepath { get; set; }
}
FileDetails Details = new FileDetails();
Details.Text = "text that you need to return";
Details.FileName = "Name of the file that you need to return";
Details.Filepath = "Path of the file that you need to return";
return View("ViewName", Details);
@model YourProjectName.Models.FileDetails
@{
    var text = Model.Text;
    var filename = Model.FileName;
    var filePath = Model.Filepath;
}


Share : facebook icon twitter icon
Eclipse now shows only a single method in an editor pane after clicking on the method in Package Explorer view, how can

Eclipse now shows only a single method in an editor pane after clicking on the method in Package Explorer view, how can


By : user3386193
Date : March 29 2020, 07:55 AM
To fix this issue Go to Preferences -> Java -> Editor and deselect "Only show the selected Java Element".
It is a hidden preference. I once had this happen to me in an entirely different circumstance and it took me hours to find the answer.
Method to return a file or a view

Method to return a file or a view


By : user6799645
Date : March 29 2020, 07:55 AM
Hope that helps IMHO:
Change your GetFile to be an ActionResult. If the file can be written
code :
public ActionResult GetFile(Int32 CategoryId)
{
  if (/*can be written*/)
  {
    using (MemoryStream stream = new MemoryStream())
    {
      using (StreamWriter writer new StreamWriter(stream))
      {
        /* writer.Write(...); */
      }
      return File(stream, "text/plain");
    }
  }
  return Redirect("~/main/Page"); // or redirectToRoute/RedirectToAction
}
How do I get my ActionResult method to return a file for download and the Partial View?

How do I get my ActionResult method to return a file for download and the Partial View?


By : Sabyasachi Dey
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Using jquery to simplify the answer
Considering an initial problem that I have a Search and Export functionality based on a filter criteria selected by the user in my view.
code :
@using (Ajax.BeginForm(..)) 
...
public PartialViewResult SearchModes([form-fields]) {
   ...
   return Partial(...)
<button id="exportButton"...></button>

$(function () {
    $("#exportButton").click(exportMyForm);
});

function exportMyForm() {
    var $form = $("form");

    // Changed the line below (see @comments)
    // var result = decodeURIComponent($.param($form.serializeFormJSON())); 
    var result = $form.serialize();

    window.location.href = "@Url.Action("MyController", "MyExportAction")?" + result;
}
public FileResult MyExportAction([form-fields]) {
   ...
   Response.AddHeader("content-disposition", "attachment; filename=SearchResults.xls");
   Response.ContentType = "application/vnd.ms-excel";
   ...
   return File(...)
Handle multiple imgeview with single onclick (View view) method

Handle multiple imgeview with single onclick (View view) method


By : TermiJAG
Date : March 29 2020, 07:55 AM
I hope this helps . I create multiple imageviews programatically by inflating one layout and set id to that imageview. all these task i perform in for loop. i want to set on click listener on these programatically created imageviews. i set onclick listener but its not working. , add onClick inside loop. like :
code :
imageview_delete_breakfast.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // do what you want for individual click
                }
            });
File Browser Dialog MVVM C#, return complete path to view model and diplay only file name in view

File Browser Dialog MVVM C#, return complete path to view model and diplay only file name in view


By : Dina Hassan
Date : March 29 2020, 07:55 AM
wish of those help It's not possible or at least very complicated if you display a shortened path or filename but let the user enter a full path…
I would use two different fields or use a OneWay Binding and let the user only select a new file with the FileOpenDialog but not let him enter a path manually…
code :
public string SortenedFolderName => Path.GetFileName(_folderName);

private string _folderName;
public string FolderName
{
   get { return _folderName; }
   set
   {
        _folderName = value;
        OnPropertyChanged(nameof(FolderName));
        OnPropertyChanged(nameof(SortenedFolderName));
    }
} 
<TextBox Text="{Binding SortenedFolderName, Mode=OneWay}" IsReadOnly="True" />
Related Posts Related Posts :
  • Why does C# also not allow empty conditions in while loops?
  • Unable to get a block of code into my regex match groups
  • What is difference between dbcontext.Add and dbcontext.AddObject
  • How do I update the file version number of C# DLL without recompiling?
  • Value for html control always null
  • HttpClient hangs when timeout is setting (Windows Phone)
  • Handle Multiple Form tag in asp.net page?
  • create word document with html content in c#
  • Confusion about Find And Replace
  • Format sms messages in Clickatell
  • Automated Function Overload
  • String to date in MS Access SQL statement gives type mismatch error
  • ShowDialog exiting on certain events
  • Is there a try Convert.ToInt32... avoiding exceptions
  • How to move wpf application into minimize tray at Window Start-up C#?
  • How to change display format of long variable?
  • How to use Addfields in MongoDB C# Aggregation Pipeline
  • MsTest TestCleanup method not called when an unhandled exception is thrown
  • missing last data when exporting gridview to excel
  • How to add array of objects to List in c#
  • Lambda Expression to order (sort) my list collection
  • Library for displaying music notation
  • How to compare two dictionaries in c# and get the output as True and False after validation
  • specify fields to be serialized with JSON
  • How do you obtain the content of a specific node using XmlDocument in C#?
  • How to ignore the first line in a csv file when you read the csv file in C#
  • c# - Problem calling public void from class
  • How to tell a class which objects it should create? Type vs. object confusion :(
  • Save CheckBox state to xml
  • WIX CAQuietExec NETSH Command Fails
  • Issues sending http put request every 60 seconds to RoR app
  • ConfigurationManager.ConnectionStrings.ConnectionString Issue
  • Real size WPF controls for printing
  • How to cancel properly?
  • C# String multiplication error
  • Using Solrnet and Assigning Attributes with Entity Framework Generated POCOs
  • Regex pattern for single backslash
  • TextBox: insert spaces for credit card number?
  • C# DLL loaded for exe-application is not found when launching similar DLL by rundll32.exe
  • Debug a Windows Service with WCF library
  • Open a file from an external assembly?
  • Servicestack RegistrationFeature Unable to bind request
  • Index was outside the bounds of the array confusion
  • Error in Xml to List code. The ':' character, hexadecimal value 0x3A, cannot be included in a name
  • I am trying to do a while loop with a string conditional statement in C#
  • C# 'Cannot access a disposed object. Object name: 'SslStream'.'
  • How to make Gecko use seperate CookieContainer per instance?
  • C# Advanced form "please wait"
  • Send and Receive data C# using network stream
  • How to discover that appsettings changed in C#?
  • Check what needs full trust
  • What is the execution order of an MVC Razor view/layout
  • Table designer (Entity Framework) is too resource intense
  • How to clean up an exception string so it can be displayed via Javascript Alert?
  • Configuring Amazon SES Feedback Notifications via Amazon SNS in ASP.NET MVC (C#)
  • C# api responce and request
  • Dynamodb putitem function not working properly
  • theme in windows phone(light or dark) using c#
  • Backup attached database file(.mdf) using c# and SQL Server
  • What is 'TextFile' and where it is uses in WPF project
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org