0

I'm trying to make a file upload to the server using Uploadify, but not working the TempData to pass variables between controllers and I have not found an error.

I'm trying passing the variables fileName and file with TempData of the controller "GetFile" to the controller "ModelCreate".

The controller "GetFile" works well, but when I check the value of "date1" and "date2" in the controller "ModelCreate" is null

I just want to make the file saved in the controller "ModelCreate"

  public string GetFile(HttpPostedFileBase file)
        {
            var fileName = this.Server.MapPath("~/Informs/" + System.IO.Path.GetFileName(file.FileName));

            if (System.IO.File.Exists(fileName))
                return "has been uploaded successfully";
            file.SaveAs(fileName);

            TempData["NameFile"] = fileName;
            TempData["File"] = file;
            return "1";
        }


        [HttpPost]
        public ActionResult ModelCreate(INFORME inform)
       {
            var date1 = TempData["NameFile"] as string;
            var date2 = TempData["File"] as HttpPostedFileBase;
            date2.SaveAs(date1);
        .
        .
        .
        .
        }

why "date1" and "date2" are null?

Blessings

kalu
  • 337
  • 2
  • 9
  • 19
  • You're asking what is wrong with the output of ModelCreate and you don't show the return value?!? – tzerb May 27 '12 at 06:05
  • 1
    Which controller action is invoked initially by Uploadify? How are you passing from the first action to the ModelCreate action? Are you redirecting? What is the `GetFile` method that you have shown? Who is calling this method? – Darin Dimitrov May 27 '12 at 06:37
  • @Darin Dimitrov initially by Uploadify is invoked "GetFile", With this controller no problem, the problem I have with the TempData :( – kalu May 27 '12 at 06:46
  • But `GetFile` doesn't look like a controller action. Controller actions must return ActionResults. `GetFile` simply returns a string. Also what's the relation with the `ModelCreate` action? How is this one called? – Darin Dimitrov May 27 '12 at 06:47
  • @Darin Dimitrov Thanks for your help, I'm new to MVC and I just want to upload a file and simultaneously send in the same form others html components for example: textbox, radio button, etc.. How I can do this? if you can help me with a complete example please... – kalu May 28 '12 at 02:48
  • @kalu, I've posted a complete example as you requested. – Darin Dimitrov May 28 '12 at 08:12

1 Answers1

2

There's not enough information to provide an answer to this question. As requested in the comments section I will provide a full example illustrating a form allowing the user to fill a couple of input fields and upload a file.

As always we start by defining the view model which will reflect the information that we want to display on the view:

public class MyViewModel
{
    [Required]
    public string TextField { get; set; }

    [DataType(DataType.MultilineText)]
    public string TextAreaField { get; set; }

    public bool CheckBoxField { get; set; }

    [Required]
    public HttpPostedFileBase FileField { get; set; }
}

Then we could have a controller with 2 actions: a GET action that simply displays the form and a POST action that processes the form information when submitted:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // there were validation errors => redisplay the view
            return View(model);
        }

        // at this stage the model is valid => we could do some processing

        // first let's save the file
        var appData = Server.MapPath("~/app_data");
        var file = Path.Combine(appData, Path.GetFileName(model.FileField.FileName));
        model.FileField.SaveAs(file);

        // then we could process the other properties
        // ...

        return Content("Thanks for submitting the data");
    }
}

and finally a strongly typed view top the view model:

@model MyViewModel

@Html.ValidationSummary(false)

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        @Html.EditorFor(x => x.TextField)
        @Html.ValidationMessageFor(x => x.TextField)
    </div>

    <div>
        @Html.EditorFor(x => x.TextAreaField)
        @Html.ValidationMessageFor(x => x.TextAreaField)
    </div>

    <div>
        @Html.CheckBoxFor(x => x.CheckBoxField)
        @Html.ValidationMessageFor(x => x.CheckBoxField)
    </div>

    <div>
        @Html.LabelFor(x => x.FileField)
        @Html.TextBoxFor(x => x.FileField, new { type = "file" })
    </div>

    <button type="submit">OK</button>
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928