简体   繁体   English

将上传的数据显示到 MVC 视图并将选定的记录导入数据库

[英]Display uploaded data to MVC view and import selected record to database

I need to import user uploaded CSV data to database.我需要将用户上传的 CSV 数据导入数据库。 Steps - 1. User upload data to MVC View 2. Display all uploaded data in the View 3. User can select the column name based on imported value 3. Import data based on user selection of the records.步骤 - 1. 用户将数据上传到 MVC 视图 2. 在视图中显示所有上传的数据 3. 用户可以根据导入的值选择列名 3. 根据用户选择的记录导入数据。

To implement this, I created below Model -为了实现这一点,我创建了下面的模型 -

public class ImportCSVData
{      
    public IEnumerable<System.Web.Mvc.SelectListItem> Names { get; set;}    // This is to display dynamic column      
    public HttpPostedFileBase File { get; set; }     
    public DataTable Data { get; set; }  
}  

In the controller, I am parsing the uploaded file (sample code)在控制器中,我正在解析上传的文件(示例代码)

    public ActionResult ImportCSVCustomer()
    {
        ImportCSVData model = new ImportCSVData();
        HttpPostedFileBase upfile = Request.Files["File"];
        model.File = upfile;            
            var dt = ParseCSVData(model);
            if (dt != null && dt.Rows.Count > 0)
            {                 
                var dcRec = new DataColumn("ShouldImport", typeof(bool));                 
                dcRec.DefaultValue = false;
                dt.Columns.Add(dcRec);         
                model.Data = dt;
                List<SelectListItem> Names = getColumnNames();
                model.Names = Names;                 
                return PartialView("_ImportedCSVCustomer", model);
            }                      
        return View();
    }

Sample View that I designed -我设计的示例视图 -

   @for (int i = 0; i < Model.Data.Rows.Count; i++)
   {
   <tr>
    <td data-val="@i">
    <input type="checkbox" name="sid" value=@Model.Data.Rows[i][Model.Data.Columns.Count - 1] class="sid">
   </td>
    @for (int j = 0; j < Model.Data.Columns.Count - 1; j++)
    {
     <td>
         @Model.Data.Rows[i][j].ToString()
         @Html.HiddenFor(Model.Data.Rows[i][j].ToString())
     </td>
     }
   </tr>
   }

I was working on the view to display, but , unable to get the model value after posting from View to Controller.我正在处理要显示的视图,但是从视图发布到控制器后无法获取模型值。 Any idea how to implement this?知道如何实现这一点吗?

You need to pass the model in the return statement.您需要在 return 语句中传递模型。 So you need:所以你需要:

return View(model)

Then in the view you have to make sure you are implementing that view by having at the top of the .cshtml file然后在视图中,您必须确保通过 .cshtml 文件的顶部实现该视图

@model xxx.ImportCSVData

Where xxx is the namespace.其中xxx是命名空间。

In all honest, I'd also put [HttpPost] on top of the controller as well to make sure you can only POST to it and I'd use the AntiForgeryToken.老实说,我还将[HttpPost]放在控制器的顶部,以确保您只能向它发送 POST 信息,并且我会使用 AntiForgeryToken。 http://peterkellner.net/2014/05/19/asp-net-mvc-forms-need-include-html-antiforgerytoken-security/ http://peterkellner.net/2014/05/19/asp-net-mvc-forms-need-include-html-antiforgerytoken-security/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM