- Messages
- 122
- Reaction score
- 0
- Points
- 26
Good Day mga ka SB tanung ko lang po bakit pag nagupload ako nang excel file nag instert sa database
pero pag minual ko yung number hindi siya nag input sa database
see my code below, I Hope na matulungan niyo po ako
Controller
[HttpPost]
public ActionResult upload(HttpPostedFileBase file, int month = 1, int year = 2017)
{
ViewBag.month = month;
ViewBag.year = year;
var List = new List<BudgetViewModel>();
if (file != null && file.ContentLength > 0)
try
{
var reader = new ExcelReader();
var path = Path.Combine(Server.MapPath("~/Content/Budget/"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
//ViewBag.Message = "File uploaded successfully : " + Path.GetFileName(file.FileName);
ViewBag.Path = Path.Combine(Server.MapPath(@"~/Content/Budget/"), Path.GetFileName(file.FileName));
List =
reader.ReadBudget(Path.Combine(Server.MapPath(@"~/Content/Budget/"),
Path.GetFileName(file.FileName)));
}
catch (Exception ex)
{
ViewBag.Message = "ERROR:" + ex.Message;
}
else
{
ViewBag.Message = "You have not specified a file.";
}
return View("Create", List);
}
//
// GET: /Budget/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Budget/Create
public ActionResult Create()
{
var departments = DepartmentDB.DistinctBudgetDeptList();
var Budgets = BudgetDb.List();
var deptbudgets = (from d in departments
join b in Budgets
on d.DEPARTMENTID equals b.DEPARTMENTID.Trim()
into a
from i in a.DefaultIfEmpty(new BudgetViewModel())
select new BudgetViewModel
{
MONTH = d.MONTH,
YEAR = d.YEAR,
DEPARTMENTID = d.DEPARTMENTID,
DATETIME = i.DATETIME,
BUDGET = i.BUDGET,
DEPARTMENTNAME = d.DEPARTMENTNAME
}).ToList();
return View(departments.ToList());
}
//
// POST: /Budget/Create
[HttpPost]
public ActionResult Create(List<BudgetViewModel> model, int year, int month)
{
var Budgets = BudgetDb.List();
var Departments = DepartmentDB.List();
try
{
foreach (var dept in model)
{
// TODO: Add insert logic here
dept.MONTH = month;
dept.YEAR = year;
dept.DATETIME = DateTime.Now;
var has = Departments.Any(d => d.DEPARTMENTID.Trim() == dept.DEPARTMENTID);
if (!has)
{
var newDept = new DepartmentModel();
newDept.DEPARTMENTID = dept.DEPARTMENTID.Trim();
if (dept.DEPARTMENTNAME.Length > 20)
{
newDept.DEPARTMENTNAME = dept.DEPARTMENTNAME.Substring(0, 20);
}
else
{
newDept.DEPARTMENTNAME = dept.DEPARTMENTNAME.Trim();
}
newDept.GSEXPNCBUDGET = 0;
newDept.GSBUDGET = 0;
newDept.PIECEPARTBUDGET = 0;
newDept.REQUESTLIMIT = 0;
DepartmentDB.SaveAdd(newDept);
}
var hasbudget = Budgets.Any(d => d.DEPARTMENTID.Trim() == dept.DEPARTMENTID && d.MONTH == month && d.YEAR == year);
if (!hasbudget)
{
BudgetDb.insert(dept);
}
}
return RedirectToAction("Index");
}
catch
{
return View(model);
}
}
//
// GET: /Budget/Edit/5
public ActionResult Edit(int id)
{
return View();
}
//
// POST: /Budget/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Budget/Delete/5
public ActionResult Delete(int id)
{
return View();
}
//
// POST: /Budget/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
Create.cshtml
@model IList<WarehouseRtoRSystem.Models.BudgetViewModel>
<div class="w3-white">
<div class="w3-container w3-teal header w3-text-white w3-border">
<div class="w3-col m12 ">
<br />
</div>
<h2 class="">Budgeting</h2>
<p class="w3-blockquote"> List of Departments</p>
<div class="w3-col m12 ">
<br />
</div>
</div>
<div class="w3-container">
<br />
<br />
<br />
<br />
@using (Html.BeginForm("upload", "Budget", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" id="file" />
<input type="submit" value="Upload file" />
}
@using (Html.BeginForm("Create","Budget", FormMethod.Post))
{
@ViewBag.Message
<br />
<br />
<br />
<br />
<label>Month </label>
<select id="month" name="month">
@{ string[] Months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; }
@for (var i = 0; i < 12; i++)
{
var m = i + 1;
if (Convert.ToInt32(ViewBag.month) == m)
{
<option value=@m selected>@Months</option>
}
else
{
<option value=@m>@Months</option>
}
}
</select>
<label>YEAR</label>
<select id="year" name="year">
@for (var c = 0; c < 1000; c++)
{
var yr = c + 2017;
if (Convert.ToInt32(ViewBag.year) == yr)
{
<option value=@yr selected>
@yr
</option>
}
else
{
<option value=@yr> @yr</option>
}
}
</select>
<div class="w3-col m12 table-responsive" style="height: 300px">
<table class="table-bordered">
<tr class="w3-teal w3-border">
<th>
DEPARTMENT CODE
</th>
<th>
DEPARTMENT NAME
</th>
<th>
BUDGET
</th>
</tr>
@for (var i = 0; i < Model.Count; i++)
{
<tr class="w3-border ">
<td style="margin: 0px; padding: 5px;">
@Html.TextBoxFor(m => m.DEPARTMENTID, new { @readonly = "readonly", @class = "form-control" })
</td>
<td style="padding: 5px;">
@Html.TextBoxFor(m => m.DEPARTMENTNAME, new { @readonly = "readonly", @class = "form-control" })
</td>
<td style="padding: 5px;">
@Html.TextBoxFor(m => m.BUDGET, new { type = "number", @class = "form-control" })
</td>
</tr>
}
</table>
</div>
<br />
<br />
<input type="submit" value="Save" class="btn btn-primary" />
}
<br />
<br />
</div>
</div>
pero pag minual ko yung number hindi siya nag input sa database
see my code below, I Hope na matulungan niyo po ako
Controller
[HttpPost]
public ActionResult upload(HttpPostedFileBase file, int month = 1, int year = 2017)
{
ViewBag.month = month;
ViewBag.year = year;
var List = new List<BudgetViewModel>();
if (file != null && file.ContentLength > 0)
try
{
var reader = new ExcelReader();
var path = Path.Combine(Server.MapPath("~/Content/Budget/"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
//ViewBag.Message = "File uploaded successfully : " + Path.GetFileName(file.FileName);
ViewBag.Path = Path.Combine(Server.MapPath(@"~/Content/Budget/"), Path.GetFileName(file.FileName));
List =
reader.ReadBudget(Path.Combine(Server.MapPath(@"~/Content/Budget/"),
Path.GetFileName(file.FileName)));
}
catch (Exception ex)
{
ViewBag.Message = "ERROR:" + ex.Message;
}
else
{
ViewBag.Message = "You have not specified a file.";
}
return View("Create", List);
}
//
// GET: /Budget/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Budget/Create
public ActionResult Create()
{
var departments = DepartmentDB.DistinctBudgetDeptList();
var Budgets = BudgetDb.List();
var deptbudgets = (from d in departments
join b in Budgets
on d.DEPARTMENTID equals b.DEPARTMENTID.Trim()
into a
from i in a.DefaultIfEmpty(new BudgetViewModel())
select new BudgetViewModel
{
MONTH = d.MONTH,
YEAR = d.YEAR,
DEPARTMENTID = d.DEPARTMENTID,
DATETIME = i.DATETIME,
BUDGET = i.BUDGET,
DEPARTMENTNAME = d.DEPARTMENTNAME
}).ToList();
return View(departments.ToList());
}
//
// POST: /Budget/Create
[HttpPost]
public ActionResult Create(List<BudgetViewModel> model, int year, int month)
{
var Budgets = BudgetDb.List();
var Departments = DepartmentDB.List();
try
{
foreach (var dept in model)
{
// TODO: Add insert logic here
dept.MONTH = month;
dept.YEAR = year;
dept.DATETIME = DateTime.Now;
var has = Departments.Any(d => d.DEPARTMENTID.Trim() == dept.DEPARTMENTID);
if (!has)
{
var newDept = new DepartmentModel();
newDept.DEPARTMENTID = dept.DEPARTMENTID.Trim();
if (dept.DEPARTMENTNAME.Length > 20)
{
newDept.DEPARTMENTNAME = dept.DEPARTMENTNAME.Substring(0, 20);
}
else
{
newDept.DEPARTMENTNAME = dept.DEPARTMENTNAME.Trim();
}
newDept.GSEXPNCBUDGET = 0;
newDept.GSBUDGET = 0;
newDept.PIECEPARTBUDGET = 0;
newDept.REQUESTLIMIT = 0;
DepartmentDB.SaveAdd(newDept);
}
var hasbudget = Budgets.Any(d => d.DEPARTMENTID.Trim() == dept.DEPARTMENTID && d.MONTH == month && d.YEAR == year);
if (!hasbudget)
{
BudgetDb.insert(dept);
}
}
return RedirectToAction("Index");
}
catch
{
return View(model);
}
}
//
// GET: /Budget/Edit/5
public ActionResult Edit(int id)
{
return View();
}
//
// POST: /Budget/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Budget/Delete/5
public ActionResult Delete(int id)
{
return View();
}
//
// POST: /Budget/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
Create.cshtml
@model IList<WarehouseRtoRSystem.Models.BudgetViewModel>
<div class="w3-white">
<div class="w3-container w3-teal header w3-text-white w3-border">
<div class="w3-col m12 ">
<br />
</div>
<h2 class="">Budgeting</h2>
<p class="w3-blockquote"> List of Departments</p>
<div class="w3-col m12 ">
<br />
</div>
</div>
<div class="w3-container">
<br />
<br />
<br />
<br />
@using (Html.BeginForm("upload", "Budget", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" id="file" />
<input type="submit" value="Upload file" />
}
@using (Html.BeginForm("Create","Budget", FormMethod.Post))
{
@ViewBag.Message
<br />
<br />
<br />
<br />
<label>Month </label>
<select id="month" name="month">
@{ string[] Months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; }
@for (var i = 0; i < 12; i++)
{
var m = i + 1;
if (Convert.ToInt32(ViewBag.month) == m)
{
<option value=@m selected>@Months</option>
}
else
{
<option value=@m>@Months</option>
}
}
</select>
<label>YEAR</label>
<select id="year" name="year">
@for (var c = 0; c < 1000; c++)
{
var yr = c + 2017;
if (Convert.ToInt32(ViewBag.year) == yr)
{
<option value=@yr selected>
@yr
</option>
}
else
{
<option value=@yr> @yr</option>
}
}
</select>
<div class="w3-col m12 table-responsive" style="height: 300px">
<table class="table-bordered">
<tr class="w3-teal w3-border">
<th>
DEPARTMENT CODE
</th>
<th>
DEPARTMENT NAME
</th>
<th>
BUDGET
</th>
</tr>
@for (var i = 0; i < Model.Count; i++)
{
<tr class="w3-border ">
<td style="margin: 0px; padding: 5px;">
@Html.TextBoxFor(m => m.DEPARTMENTID, new { @readonly = "readonly", @class = "form-control" })
</td>
<td style="padding: 5px;">
@Html.TextBoxFor(m => m.DEPARTMENTNAME, new { @readonly = "readonly", @class = "form-control" })
</td>
<td style="padding: 5px;">
@Html.TextBoxFor(m => m.BUDGET, new { type = "number", @class = "form-control" })
</td>
</tr>
}
</table>
</div>
<br />
<br />
<input type="submit" value="Save" class="btn btn-primary" />
}
<br />
<br />
</div>
</div>
Last edited: