2
Answers

how can controller pass data show data grid in mvc 4

mahmudur rifat

mahmudur rifat

8y
244
1
my project Mvc 4. Below In my Code :
 
Controller :
using Excel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UploadFileApp.Models;


namespace UploadFileApp.Controllers
{
public class UploadFileController : Controller
{

public ActionResult Index()
{
return View();
}
public ActionResult Upload()
{
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Upload(HttpPostedFileBase upload)
{
List<UploadFile> sefile = new List<UploadFile>();

if (ModelState.IsValid)
{

if (upload != null && upload.ContentLength > 0)
{

Stream stream = upload.InputStream;


IExcelDataReader reader = null;


if (upload.FileName.EndsWith(".xls"))
{
reader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else if (upload.FileName.EndsWith(".xlsx"))
{
reader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else
{
ModelState.AddModelError("File", "This file format is not supported");
return View();
}

reader.IsFirstRowAsColumnNames = true;

DataSet result = reader.AsDataSet();
//return View(result.Tables[0]);

while (reader.Read())
{

string invoice = reader.GetString(0);
DateTime dateinvoice = reader.GetDateTime(1);
DateTime datepayment = reader.GetDateTime(2);
string sender = reader.GetString(3);
string benificiary = reader.GetString(4);
string accountNumber = reader.GetString(5);
string bankBranch = reader.GetString(6);
string bankName = reader.GetString(7);
string city = reader.GetString(8);
int reference = reader.GetInt32(9);
string pin = reader.GetString(10);
string cashier = reader.GetString(11);
decimal totalPayDollar = reader.GetDecimal(12);
decimal totalPayLocal = reader.GetDecimal(13);
string branch = reader.GetString(14);
int senderPhone = reader.GetInt32(15);
int receiverPhone = reader.GetInt32(16);

UploadFile filelist = new UploadFile();
filelist.Invoice = invoice;
filelist.DateInvoice = dateinvoice;
filelist.DatePayment = datepayment;
filelist.Sender = sender;
filelist.Benificiary = benificiary;
filelist.AccountNumber = accountNumber;
filelist.BankBranch = bankBranch;
filelist.BankName = bankName;
filelist.City = city;
filelist.Reference = reference;
filelist.Pin = pin;
filelist.Cashier = cashier;
filelist.TotalPayDollar = totalPayDollar;
filelist.TotalPayLocal = totalPayLocal;
filelist.Branch = branch;
filelist.SenderPhone = senderPhone;
filelist.ReceiverPhone = receiverPhone;
//ViewBag.FileList = filelist.Invoice;
sefile.Add(filelist);

return View(sefile);
}

reader.Close();
}
else
{
ModelState.AddModelError("File", "Please Upload Your file");
}
}

return View("Upload");
}

}

 
 View  :
 
 <h2>Upload File</h2>


@using (Html.BeginForm("Upload", "UploadFile", null, FormMethod.Post, new { enctype = "multipart/form-data" }))

{
@Html.AntiForgeryToken()
@Html.ValidationSummary()

<div class="form-group">
<input type="file" id="dataFile" name="upload" />
</div>

<div class="form-group">
<input type="submit" value="Upload" class="btn btn-default" />
</div>
  ----------------- This Part Does not Work  -------------------------------------------
@model List<UploadFileApp.Models.UploadFile>
@model List < DEMO_WEBGRID.Models.OrderModel >
@ {
Layout = null;
} < !DOCTYPE html >

< html >
< head >
< meta name = "viewport"
content = "width=device-width" / >
< title > DisplayGrid < /title> < /head> < body >
< div >
@ {
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 5);
grid.Pager(WebGridPagerModes.All);
}


@grid.GetHtml(
htmlAttributes: new
{
id = "gridMapping"
},
tableStyle: "table table-bordered",
headerStyle: "info",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "gridrow",
columns: grid.Columns(
grid.Column("Invoice", "Invoice"),
grid.Column("DateInvoice", "DateInvoice Name"),
grid.Column("DatePayment", "DatePayment"),
grid.Column("Sender", "Sender"),
grid.Column("Benificiary", "Benificiary Status"),
grid.Column("BankBranch", "BankBranch"),
grid.Column("BankName", "BankName"),
grid.Column("City", "City"),
grid.Column("Reference", "Reference"),
grid.Column("Pin", "Pin"),
grid.Column("Cashier", "Cashier"),
grid.Column("TotalPayDollar", "TotalPayDollar"),
grid.Column("TotalPayLocal", "TotalPayLocal"),
grid.Column("Branch", "Branch"),
grid.Column("SenderPhone", "SenderPhone"),
grid.Column("ReceiverPhone", "ReceiverPhone")

)
) < /div> < /body> < /html>

}
 
 --------------------------------------------------------------------------------------------
 
 
 

Answers (2)