Table above from Excel.It succeed upload into ASP.NET MCV before I add a code to copy that to another model using "Loop".
controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Step_Excel_2.Models;
using System.IO;
using System.Configuration;
using System.Data.OleDb;
using System.Data;
namespace Step_Excel_2.Controllers
{
public class HomeController : Controller
{
[HttpGet]
// GET: Home
public ActionResult Index()
{
return View(new List<Mahkota>());
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase PostedFile)
{
List<Mahkota> Paksi_XY = new List<Mahkota>();
if (PostedFile != null)
{
string path = Server.MapPath("~/Test/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filepath = path + Path.GetFileName(PostedFile.FileName);
string extension = Path.GetExtension(PostedFile.FileName);
PostedFile.SaveAs(filepath);
string ConStr = string.Empty;
switch (extension)
{
case ".xls":
ConStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx":
ConStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
ConStr = string.Format(ConStr, filepath);
OleDbConnection connExcel = new OleDbConnection(ConStr);
{
OleDbCommand cmdExcel = new OleDbCommand();
{
OleDbDataAdapter odaExcel = new OleDbDataAdapter();
{
DataTable dt = new DataTable();
cmdExcel.Connection = connExcel;
// Get Name first worksheet.
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
// Read data in Sheet.
connExcel.Open();
cmdExcel.CommandText = "SELECT * FROM [" + SheetName + "]";
odaExcel.SelectCommand = cmdExcel;
odaExcel.Fill(dt);
connExcel.Close();
ViewBag.header1 = dt.Columns[1].ColumnName.ToString().Replace("#", ".").Replace("_", "");
ViewBag.header7 = dt.Columns[7].ColumnName.ToString().Replace("#", ".").Replace("_", "");
ViewBag.header11 = dt.Columns[11].ColumnName.ToString().Replace("#", ".").Replace("_", "");
foreach (DataRow row in dt.Rows)
{
var test = new Mahkota();
{
test.Sample_Id = row[1].ToString().Replace("#", ".");
}
// .
if (String.IsNullOrEmpty(row[7].ToString()))
{
test.P_cps = Convert.ToDecimal(null);
}
else
{
test.P_cps = Convert.ToDecimal(row[7].ToString().Replace("#", "."));
}
// .
if (String.IsNullOrEmpty(row[11].ToString()))
{
test.P_mgL = Convert.ToDecimal(null);
}
else
{
test.P_mgL = Convert.ToDecimal(row[11].ToString().Replace("#", "."));
}
var vm = new MahkotaVm();
for (int i = 0; i < Paksi_XY.Count; i++)
{
vm.Mahkotas.Add(new Mahkota { P_cps = vm.Av_cps[i], P_mgL = vm.Av_mgL[i] });
}
Paksi_XY.Add(vm);
}
}
}
}
}
return View(Paksi_XY);
}
}
}
2. model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Step_Excel_2.Models
{
public class Mahkota
{
public string Sample_Id { get; set; }
public decimal P_cps { get; set; }
public decimal P_mgL { get; set; }
}
public class MahkotaVm
{
public List<Mahkota> Mahkotas { get; set; }
public decimal Av_cps { get; set; }
public decimal Av_mgL { get; set; }
}
}
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
At the end of the loop, the value of vm.Av_cps is the value of P_cps in the last set of data of Paksi_XY.
In other words, the loop traversesevery piece of data, but every time the subsequent dataoverwrites the existing value in vm.Av_cps.
If you want to loop Paksi_XY to get P_cps and assign it to Av_cps, then you should modify your code like this:
Model
public class MahkotaVm
{
public List<Mahkota> Mahkotas { get; set; }
public List<decimal> Av_cpslist { get; set; }
public List<decimal> Av_mgLlist { get; set; }
}
Controller
vm.Mahkotas = Paksi_XY;
vm.Av_cpslist = new List<decimal>();vm.Av_mgLlist = new List<decimal>();
for (int i = 0; i < Paksi_XY.Count; i++)
{
vm.Av_cpslist.Add(Paksi_XY[i].P_cps);
vm.Av_mgLlist.Add(Paksi_XY[i].P_mgL);
}
Here is the result.:
Note:
I made a guess based on the name of "Av_cps": Do you want to get the average of all P_cps?
If this is the case, there is no need to write a loop, you can write the code like this:
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
@for (var i = 0; i < Model.Av_cpslist.Count; i++) { var item1 = Model.Av_cpslist[i]; var item2 = Model.Av_mgLlist[i]; <tr> <td>@item1</td> <td>@item2</td> </tr> }
Best Regards,
YihuiSun
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
18 Points
56 Posts
Pass model into another model
Nov 15, 2020 03:52 AM|sy_60@yahoo.com|LINK
1200.000
Table above from Excel.It succeed upload into ASP.NET MCV before I add a code to copy that to another model using "Loop".
2. model
3. view
What try to do is "How to show the result from var "test" into var "vm" Using "Loop".
The result may be like this
950.650
(cps)
(mg/L)
somebody can show the actual code can be.
Contributor
2980 Points
851 Posts
Re: Pass model into another model
Nov 16, 2020 06:25 AM|YihuiSun|LINK
Hi sy_60,
According to your needs, I modified the code you provided, you can refer to it.
Controller
View
Here is the result.
Best Regards,
YihuiSun
Member
18 Points
56 Posts
Re: Pass model into another model
Nov 20, 2020 05:54 AM|sy_60@yahoo.com|LINK
Thank you YihuiSun .
If I want the Loop method in Controller That how to do?.
I test several code but not correctly true.
This code bellow , Loop to the end not one by one.
Contributor
2980 Points
851 Posts
Re: Pass model into another model
Nov 20, 2020 08:40 AM|YihuiSun|LINK
Hi sy_60,
Best Regards,
YihuiSun
Member
18 Points
56 Posts
Re: Pass model into another model
Nov 21, 2020 03:45 AM|sy_60@yahoo.com|LINK
Thank very very much YihuiSun for your guide and explanation.It's useful.
One more thing I need some advcie and if can how the code can be write.
Why we still use this code;
and if I want use this code in view , How?
Thank YuiShun for helping;
Contributor
2980 Points
851 Posts
Re: Pass model into another model
Nov 23, 2020 07:54 AM|YihuiSun|LINK
Hi sy_60,
You can modify it like this:
Best Regards,
YihuiSun
Member
18 Points
56 Posts
Re: Pass model into another model
Nov 24, 2020 07:26 AM|sy_60@yahoo.com|LINK
Thank you very much YihuiSun.