locked
Index form to contain import and export to excel actions RRS feed

  • Question

  • User-1202579601 posted

    @model IEnumerable<StudentAttendance.Models.Staff>
    <div class="row">
    <div class="col-md-3">
    <h4 style="display: inline">Staff List </h4>
    <h4 style="display: inline"><a asp-action="Create">Create New</a></h4>
    </div>
    <div class="col-md-7">
    <form asp-action="Index" method="get" enctype="multipart/form-data">
    <div class="form-actions no-color">
    <div class="col-md-3" style="margin-top:-2px">
    Find by Dept name:
    </div> <div class="col-md-4">
    <input type="text" name="SearchString" class="form-control" style="height:25px;background-color:lavender" />
    </div><div class="col-md-2">
    <input type="submit" value="Search" class="btn btn-default" />
    </div><div align="right" class="col-md-3">
    <a asp-action="Index" style="background-color:lavender">Back to full List</a>
    </div>
    </div>
    </form>
    </div>
    <a asp-action="Export">Export</a>
    <form method="post" enctype="multipart/form-data">
    <div class="col-md-2">
    <input class="form-control" type="file" name="file" />
    <a asp-action="Import">Import</a>
    </div>
    </form>
    </div>
    <table class="table">
    <thead >
    <tr>
    <th>
    @Html.DisplayNameFor(model => model.Name)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.StaId)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.Contact)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.Email)
    </th>
    <th></th>
    </tr>
    </thead>
    <tbody>
    @foreach (var item in Model)
    {
    <tr>
    <td>
    @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.StaId)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.Contact)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.Email)
    </td>
    <td>
    @if (User.IsInRole("Manager"))
    {<a asp-action="Edit" asp-route-id="@item.Id">Edit</a>} |
    <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
    @if (User.IsInRole("Admin"))
    {<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>}
    </td>
    </tr>
    }
    </tbody>
    </table>

    Index.cshtml of StaffController is shown above. I want to accomodate import funcion also. Is it possible. I have underlined the problematic tags

    Saturday, March 21, 2020 1:48 PM

All replies

  • User-2054057000 posted

    You can add import excel file function see this tutorial. Moreover change the form tags to:

    <form asp-action="Index" method="post" enctype="multipart/form-data">
    //... export function code
    </form>
    
    <form asp-action="Index_Import" method="post" enctype="multipart/form-data">
    //... import function code
    </form>

    Sunday, March 22, 2020 5:39 AM
  • User711641945 posted

    Hi pmdrait,

    If you want to import the excel files to your project,here is a demo like below:

    <form method="post" asp-action="Import" enctype="multipart/form-data">
        <div class="col-md-2">
            <input class="form-control" type="file" name="file" />
            <input type="submit" value="Import" />
        </div>
    </form>

    Controller:

    public class HomeController : Controller
    {
        //for asp.net core 3.x
        private IWebHostEnvironment _env;
        //for asp.net core 2.2
        //private IHostingEnvironment _env;
        public HomeController(IWebHostEnvironment env)
        {
            _env = env;
        }
        public IActionResult Index()
        {
    //do your stuff... return View(); } [HttpPost] public async Task<IActionResult> Import(IFormFile file) { var fileName = System.IO.Path.GetFileName(file.FileName); var filePath = System.IO.Path.Combine(_env.WebRootPath, fileName); if (file.Length > 0) { using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream);//this method would upload file to wwwroot folder } } return View("Index"); } }

    Best Regards,

    Rena

    Monday, March 23, 2020 9:07 AM