0

i'm new at this tecnology and i'm having some trouble passing a list of an excel that i imported to my application, here's the code:

The problems is that the model in the Create Controller comes out null so i cant save into the database.

I can't save it before, in the uploadcomplete action because i intend to edit the values before save into the data base.

 [HttpPost]
  public ActionResult Index(HttpPostedFileBase excelFile)
    {
        if (excelFile != null)
        {
            //Save the uploaded file to the disc.
            string savedFileName = Server.MapPath("~/UploadedExcelDocuments/" +   excelFile.FileName);
            excelFileHandler.ImportExcel(savedFileName, excelFile); 
            return RedirecToAction("UploadComplete",excelFileHandler.DataToEdit);        
        }
        else { return RedirectToAction("Error", "Upload"); }
    }

    public ActionResult UploadComplete(List<Persona> DataToEdit) // This comes out null so i cant render the view now
    {
        return View();
    }

    [HttpPost]
    public ActionResult UploadComplete(IEnumerable<ExcelImport.Persona> model)
    {
        return View();
    }

    public ActionResult Create(IEnumerable<ExcelImport.Models.Person> model) 
    {
        using (ExcelimportDBTestEntities context = new ExcelimportDBTestEntities())
        {
            foreach (ExcelImport.Models.Person person in model)
            {
                Persona newPerson = new Person();
                newPersona.Id = person.Id;
                newPersona.Birthday= persona.Birthday;
                newPersona.LastName= persona.LastName;
                newPersona.Name = persona.Name;
                context.Persons.AddObject(newPersona);
                context.SaveChanges();
            }
            return View();
        }
    }

This is my View, there must be something wrong here

@model IEnumerable<ExcelImport.Models.Person>

@{
   ViewBag.Title = "UploadComplete";
}
<h2>UploadComplete</h2>
@Html.BeginForm(){
<table>
    <tr>
        <th>
            ID
        </th>
        <th>
            Name
        </th>
        <th>
            Last Name
        </th>
        <th>
            Birthday
        </th>
        <th>
            Options
        </th>
    </tr>
  @foreach (var item in Model) {   
    @Html.HiddenFor(model => item)
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Birthday)
        </td>
        <td>

        </td>
    </tr>
    }

    </table>
   <input type="submit" value="Upload!"/>
  }

EDIT: i was tired yesterday so i put some... lets go whit "test" that i was doing by error, now this is what i really want to do. I got an Index View that upload the file and send to the post Index Controller, from there i want to send the list to my UploadComplete Controller, so i can render the UploadComplete View (the list comes out null), and in the post of that Action i want to send the model that i render in the UploadComplete View, to my create controller, so i can storage my data into the database. And as i said before i cant save it into the datebase in the index action becouse i intend to edit this data in the uploadcomplete view.

Thanks in advance, Regards.

devMd
  • 49
  • 9
  • I'm confused. How are you uploading anything, all your data is display only. Why do you even have a form? Is DataToEdit a collection of ExcelImport.Models.Person? You say this is a create form? But the title is UploadComplete? – Erik Funkenbusch Sep 20 '12 at 21:31
  • i edit the post, check what a write. – devMd Sep 21 '12 at 14:03

1 Answers1

0

As I can see in your code:

  • There is no [HttpPost] attribute
  • Form action is GET
  • Incorrect page rendering (hidden elemens)

Look at this example. It shows how lists could be binded on POST.

Community
  • 1
  • 1
webdeveloper
  • 17,174
  • 3
  • 48
  • 47
  • @user1687202 `return RedirecToAction("UploadComplete",excelFileHandler.DataToEdit);` is just 302 redirect in browser, in situations like [this](http://stackoverflow.com/questions/2324044/net-mvc-calling-redirecttoaction-passing-a-model) it converted to hyperlink. Look at `TempData`. – webdeveloper Sep 21 '12 at 14:58
  • THANK YOU! that's what i was loking for. – devMd Sep 21 '12 at 15:22
  • got a question i got to pass the list in the index to the upload controller and render it, but how can i pass the lets say final list to the post upload controller, it comes out null (is the same code from the view) – devMd Sep 21 '12 at 15:40
  • @user1687202 simple `foreach` wont work in this situation, because it create wrong names. Create `DisplayTemplate` with hidden inputs and then binder will fill your model. The link I gave you shows how it could be done. – webdeveloper Sep 21 '12 at 16:01
  • when u say hidden fields its something like Html.HiddenFor(model => item.id)? If it is i really dont understand how can put together my model, remember that i dont have the data in DB. And i need the foreach to display my table so i can be able to display and edit that in the client side... – devMd Sep 21 '12 at 18:30
  • @user1687202 It's simple, in your example you receive (example): Text, Text, Text. If you use templates: [0].Text, [1].Text, [2].Text. First can't be binded as list, second could. In other words you store all data in view with `HiddenFor` and on submit send them. – webdeveloper Sep 21 '12 at 18:46