I'd like to change a partial view to be rendered on click. I have spent a lot of time searching the solution on google.
The part is rendered while loading a main page.
<div id="tableContent">
@{Html.RenderPartial("GetTableView", Model);}
</div>
I'd like to have this paritial loaded on click, something like this:
<div id="tableContent">
<input type="button" class="btn btn-default"
onclick="Render Partial "GetTableView" in tableContent (replace the content)" />
</div>
My Model
public class ImportModel
{
public class ImportFileDetails
{
[Display(Name = "File Name")]
public string Filename { get; set; }
[Display(Name = "Total Columns")]
public int TotalColumns { get; set; }
[Display(Name = "Total Rows")]
public int TotalRows { get; set; }
[Display(Name = "Import Rows")]
public int ImportRows { get; set; }
public List<string> Header { get; set; }
public ICollection<RepCodesFile> RepCodesFile { get; set; }
}
}
public class RepCodesFile
{
public string RepCode { get; set; }
public string Description { get; set; }
}
My Controller
public ActionResult ImportFile(string ImportFile)
{
ImportModel.ImportFileDetails File = new Models.ImportModel.ImportFileDetails();
// Model is passed to View corectly
return View(File);
}
My Main View
@model Models.ImportModel.ImportFileDetails
@{
ViewBag.Title = "Import";
}
<h2>@ViewBag.Title</h2>
<hr />
@if (Model.Filename != null || Model.ImportRows == 0)
{
<div id="tableContent">
@{Html.RenderPartial("GetTableView", Model);}
</div>
}
else
{
<h4 class="text-warning">No data for import</h4>
}
My Partial
@model Models.ImportModel.ImportFileDetails
<table id="filetable" class="table table-bordered pagination" cellspacing="0">
<thead>
<tr>
@foreach (string item in Model.Header)
{
<th>@item</th>
}
</tr>
</thead>
<tbody>
foreach (AutogemCMS.Models.RepCodesFile item in Model.RepCodesFile)
{
<tr>
<td>@item.RepCode</td>
<td>@item.Description</td>
</tr>
}
</tbody>
</table>
Please help.
Thanks
Loading GIF solution: It's not what i wanted but good enough for me.
Controller:
if (Request.IsAjaxRequest())
{
return PartialView("GetTableView", File);
}
else
{
return View(File);
}
Main View:
<div id="tableContent">
<img src="~/Content/images/ajax-loader.gif" class="center-block img-responsive" />
</div>
$(function () {
$.ajax({
url: '/Import/ImportFile?ImportFile=@Model.Filename',
dataType: 'html',
success: function (data) {
$('#tableContent').html(data);
}
});
});