I need to pass list of Objects. I am passing the data threw ajax call, ajax returns the results as expected, so the ajax call gets the correct results but partial view won't render.
Controller
[HttpPost]
public ActionResult GetXlFile()
{
List<ListMatchDetails> lstPreview = new List<ListMatchDetails>();
if (System.Web.HttpContext.Current.Request.Files.AllKeys.Any())
{
var xlFile = System.Web.HttpContext.Current.Request.Files["FileToPreview"];
HttpPostedFileBase filebase = new HttpPostedFileWrapper(xlFile);
if (null != filebase && filebase.ContentLength > 0)
{
if (String.Compare(filebase.ContentType, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", true, System.Globalization.CultureInfo.InvariantCulture) == 0)
{
using (Stream stream = filebase.InputStream)
{
IExcelDataReader reader = null;
if (filebase.FileName.EndsWith(".xls"))
{
reader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else if (filebase.FileName.EndsWith(".xlsx"))
{
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
reader.IsFirstRowAsColumnNames = true;
DataSet dsResult = reader.AsDataSet();
DataTable dtResult = dsResult.Tables[0];
if (dtResult.Rows.Count > 0)
{
foreach (DataRow dr in dtResult.Rows)
{
ListMatchDetails lmd = new ListMatchDetails();
lmd.FirstName = (dr[0] != DBNull.Value) ? dr[0].ToString() : string.Empty;
lmd.LastName = (dr[1] != DBNull.Value) ? dr[0].ToString() : string.Empty;
lstPreview.Add(lmd);
}
}
reader.Close();
}
}
}
}
return PartialView("_ExcelGrid", lstPreview);
}
view
@using app.Models;
@model IEnumerable<ListMatchDetails>
@{
if (Model.Count() > 0)
{
ListMatchDetails row = Model.FirstOrDefault();
<table class="table table-hover table-responsive scrollable table-striped ">
<thead id="tableHeader">
<tr>
<td>
@Html.LabelFor(x => row.FirstName)
</td>
<td>
@Html.LabelFor(x => row.LastName)
</td>
</tr>
</thead>
<tbody class="pre-scrollable">
@foreach (var record in Model)
{
<tr>
<td>
@Html.ValueForModel(record.FirstName)
</td>
<td>
@Html.ValueForModel(record.LastName)
</td>
</tr>
}
</tbody>
</table>
}
}
jquery:
$('#btnPreview').click(function () {
var formData = new FormData();
var files = $("#btnbrowse").get(0).files;
if (files.length > 0) { formData.append("FileToPreview", files[0]); }
$.ajax({
url: '/ListMatch/GetXlFile',
type: 'POST',
dataType: 'json',
data: formData,
processData: false,
contentType: false,
success: function (result) {
//$('#record').html(result)
$('._ExcelGrid').json(result);
},
error: function () {
//alert('Click Called');
}
});
});