问题描述
这是我的代码。
here is my code.
//controller code
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult GetJsonData(FormCollection formCollection)
{
var usersList = new List<User>();
if (Request != null)
{
HttpPostedFileBase file = Request.Files["UploadedFile"];
if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
{
string fileName = file.FileName;
string fileContentType = file.ContentType;
byte[] fileBytes = new byte[file.ContentLength];
var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
using (var package = new ExcelPackage(file.InputStream))
{
var currentSheet = package.Workbook.Worksheets;
var workSheet = currentSheet.First();
var noOfCol = workSheet.Dimension.End.Column;
var noOfRow = workSheet.Dimension.End.Row;
for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
{
var user = new User();
user.FirstName = workSheet.Cells[rowIterator, 1].Value.ToString();
user.LastName = workSheet.Cells[rowIterator, 2].Value.ToString();
usersList.Add(user);
}
}
}
}
return View("GetJsonData",usersList);
}
}
}
//Index View
<h2>Index</h2>
@using (Html.BeginForm("GetJsonData", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
<tr>
<td>File:</td>
<td>
<input type="file" name="UploadedFile" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="Submit" value="Submit" id="btnsave" />
</td>
</tr>
</table>
}
<div id="example1" class="hot handsontable htColumnHeaders"></div>
<script type="text/javascript">
$('#btnsave').click(function () {
var url = '/Home/GetJsonData';
$.get(url,
null,
function (data) {
debugger;
var container = document.getElementById('#example1');
var hot = new Handsontable(container, {
data: data,
rowheader: true,
colheader: true
});
});
});
</script>
//Jsondat view
<h2>GetJsonData</h2>
<div id="example1" class="hot handsontable htColumnHeaders"></div>
(document).ready(function () {
var url = '/Home/GetJsonData';
$.get(url,
null,
function (data) {
var container = document.getElementById('#example1');
var hot = new Handsontable(container, {
data: data,
rowheader: true,
colheader: true
});
debugger;
});
});
</script>
我尝试了什么:
i尝试过post和get请求,如果在getJsondata动作中使用return json结果我得到了两条记录。
GetJsonData
What I have tried:
i have tried with post and get request, if use return json result in getJsondata action I'm getting the two records.
GetJsonData
< script type =text / javascript>
$(document).ready(function() {
var url ='/ Home / GetJsonData';
$ .get(url,
null,
函数(数据){
var container = document.getElementById('#example1');
var hot = new Handsontable(container) ,{
数据:数据,
rowheader:true,
colheader:true
});
调试器;
});
});
<script type="text/javascript">
$(document).ready(function () {
var url = '/Home/GetJsonData';
$.get(url,
null,
function (data) {
var container = document.getElementById('#example1');
var hot = new Handsontable(container, {
data: data,
rowheader: true,
colheader: true
});
debugger;
});
});
推荐答案
//Jsondat view
<h2>GetJsonData</h2>
<div id="example1" class="hot handsontable htColumnHeaders"></div>
(document).ready(function () {
var url = '/Home/GetJsonData';
我尝试了什么:
i尝试过post和get请求,如果在getJsondata动作中使用return json结果我得到了两条记录。
GetJsonData
What I have tried:
i have tried with post and get request, if use return json result in getJsondata action I'm getting the two records.
GetJsonData
< script type =text / javascript>
<script type="text/javascript">
这篇关于我是mvc的新手,我正在尝试实现handontable,但是在我调用handontable时获取null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!