对我的控制器设计没有 100% 的感觉

发布于 2024-10-19 17:53:37 字数 2417 浏览 1 评论 0原文

基本上,我上传一个 Excel 文件并解析信息,然后在视图中显示解析的内容。

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 = "~/UploadedExcelDocuments/" + 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();
        }
    }
}

不那么自信这是最好的方法。 MVC3 资深人士对这位有抱负的高级程序员有什么建议吗? :)

我是否应该调用另一个操作“UploadComplete”并让其调用 UploadComplete 视图,而不是直接从 [POST]Index 操作调用视图?我什么时候知道是使用一种方法还是另一种方法?

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 = "~/UploadedExcelDocuments/" + 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白衬杉格子梦 2024-10-26 17:53:37

我认为 Post-Redirect-Get 模式非常适合这里。例如,您可以使用 TempData 传递“人员”数据。

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

同展鸳鸯锦 2024-10-26 17:53:37

我可能会减少索引操作中的代码以简单地保存文件,并重定向到另一个负责从文件中读取数据并显示它的操作。

可以使用路由值的第三个参数调用 RedirectToAction,该路由值可以标识要读取的文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文