开发者

Not feeling 100% about my Controller design

Basically, I'm uploading an excel file and parsing the information then displaying what was parsed in a view.

using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.Mvc;
using QuimizaReportes.Models;
using System.Collections.Generic;
using System;

namespace QuimizaReportes.Controllers
{
    public class UploadController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase excelFile)
        {
            if (excelFile != null)
            {
                //Save the uploaded file to the disc.
                string savedFileName = "~/UploadedExcelDoc开发者_如何学Gouments/" + excelFile.FileName;
                excelFile.SaveAs(Server.MapPath(savedFileName));

                //Create a connection string to access the Excel file using the ACE provider.
                //This is for Excel 2007. 2003 uses an older driver.
                var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;", Server.MapPath(savedFileName));

                //Fill the dataset with information from the Hoja1 worksheet.
                var adapter = new OleDbDataAdapter("SELECT * FROM [Hoja1$]", connectionString);
                var ds = new DataSet();
                adapter.Fill(ds, "results");
                DataTable data = ds.Tables["results"];

                var people = new List<Person>();

                for (int i = 0; i < data.Rows.Count - 1; i++)
                {
                    Person newPerson = new Person();
                    newPerson.Id = data.Rows[i].Field<double?>("Id");
                    newPerson.Name = data.Rows[i].Field<string>("Name");
                    newPerson.LastName = data.Rows[i].Field<string>("LastName");
                    newPerson.DateOfBirth = data.Rows[i].Field<DateTime?>("DateOfBirth");

                    people.Add(newPerson);
                }

                return View("UploadComplete", people);
            }

            return RedirectToAction("Error", "Upload");                      
        }

        public ActionResult Error()
        {
            return View();
        }
    }
}

Not feeling so confident this is the best approach. Any suggestion any of you MVC3 vets have for this aspiring senior programmer? :)

Should I call another Action, "UploadComplete" and have that callthe UploadComplete view instead of calling a View directly from the [POST]Index action? When do I know whether to use one approach or the other?


I think Post-Redirect-Get pattern would perfectly apply here. You can pass "people" data using TempData for example.


I would probably reduce the code in the Index action to simply saving the file, and redirect to another action that takes care of reading the data out of the file and displaying it.

RedirectToAction can be called with a third parameter for route values which could identify the file for reading.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜