Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submitting form and pass data to controller method of type FileStreamResult

I have an mvc form (made from a model) which when submitted, I want to get a parameter I have the code to set the form and get the parameter

using (@Html.BeginForm("myMethod", "Home", FormMethod.Get, new { id = @item.JobId })){ } 

and inside my home controller I have

    [HttpPost]     public FileStreamResult myMethod(string id)     {          sting str = id;      } 

However, I always get the error

The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

When I omit the [HttpPost], the code executes file but the variables str and id are null. How can I fix this please?

EDIT

Can this be caused because myMethod in the controller is not an ActionResult? I realized that when I have a method of type Actionresult where the method is bound to a view, everything works well. But the type FileStreamresult cannot be bound to a View. How can I pass data to such methods?

like image 237
jpo Avatar asked Jan 02 '13 15:01

jpo


People also ask

How do you pass data from controller model?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

How pass data from controller controller in ASP NET MVC?

TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller. TempData stores the data temporarily and automatically removes it after retrieving a value. TempData is a property in the ControllerBase class.


1 Answers

When in doubt, follow MVC conventions.

Create a viewModel if you haven't already that contains a property for JobID

public class Model {      public string JobId {get; set;}      public IEnumerable<MyCurrentModel> myCurrentModel { get; set; }      //...any other properties you may need } 

Strongly type your view

@model Fully.Qualified.Path.To.Model 

Add a hidden field for JobId to the form

using (@Html.BeginForm("myMethod", "Home", FormMethod.Post)) {        //...         @Html.HiddenFor(m => m.JobId) } 

And accept the model as the parameter in your controller action:

[HttpPost] public FileStreamResult myMethod(Model model) {     sting str = model.JobId; } 
like image 114
Forty-Two Avatar answered Nov 01 '22 08:11

Forty-Two