Is it possible to change view content without performing reload - asp.net-core

I need to create view that contains table with pagination, sorting and search. I'm getting my data from backend api and then I'm passing it to the view.
My action method looks like this:
public async Task<IActionResult> Home(int page = 1)
{
var list = await _service.Get<List<Model.Rezervacija>>(page);
ViewBag.Page = page;
return View(list);
}
And I have this view:
#using eBiblioteka.Model
#model List<Rezervacija>
#{
ViewBag.Title = "Home";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="view-all">
#await Html.PartialAsync("_ViewAll", Model, null)
</div>
<a asp-action="Home" asp-route-page="#(ViewBag.Page - 1)">Previous</a>
<a asp-action="Home" asp-route-page="#(ViewBag.Page + 1)">Next</a>
And what view looks like:
And that all works. Don't pay attention for displaying pages number like this and changing with previous and next, I just wanted to simplify my question. So when i click next page, data changes and table also update very fast and that isn't problem.
But i see in tab that page is reloaded:
.
So is there any solution in asp .net core to do this without seeing that page reload. I need to call my action again to change this param "page" and then fetch new data from api without see that ugly page reload.
Thank you for all solutions.

So you want to create view that contains table with pagination, sorting and search without reloading.so you should use jquery for this.update your code like below:-
#using eBiblioteka.Model
#model IEnumerable<Rezervacija>
<div>
<table class="table table-striped border" id="myTable">
<thead>
<tr class="table-info">
<th>
#Html.DisplayNameFor(c => c.Name)
</th>
<th>
#Html.DisplayNameFor(c => c.SpecialTagId)
</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td> #item.Name </td>
<td> #item.SpecialTag.Name </td>
<td>
<partial name="_ButtonPartia3" model="#item.Id" /> //you can use partial view for delete or add data.
</td>
</tr>
}
</tbody>
</table>
</div>
<br /> <br />
#section scripts{
<script src="https://cdn.datatables.net/1.11.2/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready( function () {
$('#myTable').DataTable();
});
</script>
}
And Add this below link in your _Layout.cshtml:-
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.2/css/jquery.dataTables.min.css">
And your output will be like this:-
So when you will go from one page to another page, it will not reload because you used jquery. and it's already sorted and you can also search as needed.

You can use some excellent front-end frameworks. I now recommend you use layui, this is a Chinese community, of course you can also choose other ones. When this layui is paging, the URL is like the following format.
https://www.layui.com/demo/table/user/?page=2&limit=10
In this way, the .net core api can be called to perform partial refresh without reloading the entire page.

Related

EditForm within a table does not render

Blazor vRC1
There appears to be subtleties of the EditForm component, where it will not render its contents in certain markup situations. For example, when an EditForm is placed within the <table> tags, nothing happens.
<table>
<thead>...</thead>
<EditForm Model="MyModel">
#foreach(var item in MyModel.Items)
{
<tr><td>....</td></tr>
}
</EditForm>
</table>
However wrapping the <table> with the EditForm everything renders as expected.
<EditForm Model="MyModel">
<table>
<thead>...</thead>
#foreach(var item in MyModel.Items)
{
<tr><td>....</td></tr>
}
</table>
</EditForm>
I'm fine with the latter, however if the rendering engine is unable to handle the first example, it would be nice if it'd throw some sort of error to warn the developer the situation is not supported.
Avoiding making form as a child element of a table . As a workaround , you can try add a <div> and move EditForm inside it(although it's not correct to put div nested in form ) , or you can put form inside table cell(inside <td> tag) .
<table>
<thead>...</thead>
<div>
<EditForm Model="MyModel">
#foreach(var item in MyModel.Items)
{
}
</EditForm>
</div>
</table>
Or :
<table>
<thead>...</thead>
<tr>
<td>
<EditForm Model="MyModel">
#foreach(var item in MyModel.Items)
{
}
</EditForm>
</td>
</tr>
</table>
But of course , it's better to have your table inside form .
when an EditForm is placed within the tags, nothing happens.
That is because
<table>
<EditForm Model="MyModel">
</EditForm>
</table>
translates to
<table>
<form>
</form>
</table>
Blazor outputs this alright but most browsers won't render it. It is invalid HTML.
Putting a <div> around the form , like in #NanYu's answer, is a hack but it seems to work. Putting a <form> inside a <td> is completely valid.
So: not a Blazor / Razor issue, just invalid html.
it would be nice if it'd throw some sort of error to warn the developer the situation is not supported.
Building all (for different Browsers) into the razor engine would be a lot of work and seems unnecessary.

View is posting a empty object on submit when expecting the modified object

I have a View that lets users edit data that is fetched from a database and converted into a DataTable (for simplicity since the data can get really complicated and deep).
The issue is that when I POST the data back to the responsible Controller the controller receives the DataTable object but it's empty, e.g. the changes made by a user never get back to the Controller and cannot be saved to Database.
I am at most intermediate at web programming so I appreciate straight answers or direct pointers.
View:
#model System.Data.DataTable
#{
ViewData["Title"] = "Edit";
Layout = "~/Views/Shared/_LayoutAdminlte.cshtml";
}
#using (Html.BeginForm("EditSave", "Recipe", FormMethod.Post, new { #id = "Properties-Form" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
<h2>#Html.DisplayFor(m => Model.TableName)</h2>
</div>
<div class="box box-primary">
<table id="Properties" class="table table-condensed">
<thead>
<tr>
#foreach (System.Data.DataColumn col in Model.Columns)
{
<th>
#Html.DisplayFor(m => col.Caption)
</th>
}
</tr>
</thead>
<tbody>
#foreach (System.Data.DataRow row in Model.Rows)
{
<tr>
#foreach (var cell in row.ItemArray)
{
<td>
#if (row.ItemArray.ToList().IndexOf(cell) == 0)
{
#cell.ToString()
}
else
{
<input asp-for="#cell" />
}
</td>
}
</tr>
}
</tbody>
</table>
</div>
<div class="box-footer">
<!-- id="Save" -->
<input class="btn btn-primary pull-right" type="submit" value="Spara" id="Save" />
<i class="fa fa-show"></i> Visa
<i class="fa fa-show"></i> Avbryt
</div>
</div>
}
#section Scripts{
#await Html.PartialAsync("_ValidationScriptsPartial")
}
Controller:
[HttpPost]
public ActionResult EditSave(DataTable model)
{
Debugger.Break();
return View("Edit", model);
}
This <input asp-for="#cell" /> is your problem.
If you run your program and use the developer tools to inspect the html generated for the input fields, you will notice the generated html attributes.
the particular one that you should pay attention to is the name attribute.
The way model binding works in .net core when you send data to the controller.
public IActionResult DoSomething(Model model) { ... }
you would have match the name attribute to the property of the object. Example:
<input name="model.Level" value="8999" />
so then you would get var level = model.Level and level will be 8999.
So you have to take care when you use the asp-for it's not doing as much heavy lifting as you think. Always check the html generated.
PS
Don't use DataTables, other developers will throw rocks at you. Don't be lazy map to an actual object or use an ORM, programs quickly become unmaintainable if you use them to dynamically store data.

How can I render 3 partial views with the data on selecting an item in the listbox?

In my project, I want to extract all the fields, facilities, tanks from db based on BlockID. For each category like fields, facilities, tanks I prepared a partial view. When I run the solution, I am able to retrieve only the fields related to block in the dropdown control. I want to display all the fields' data related to that block in the partialview and also fill them in the dropdown. I could see the data in .cshtml files, but I am unable to render them on UI. Please help me.
// Controller Action
public PartialViewResult FieldsOfBlock(long blockid)
{
FacilityModel bkvm = new FacilityModel();
var fields = bkvm.FieldList(blockid); // comes from backend
ViewBag.Data = fields;
return PartialView("_PartialField");
}
// PartialView
#model List<ABC.Models.Field>
#{
var data = ViewBag.Data as List<ABC.Models.Field>;
}
<div class="table-responsive col-lg-12 margintop20">
<table class="table table-bordered table-hover" id="myTableDataField">
<thead>
<tr>
<th>Field Name</th>
</tr>
</thead>
<tbody>
#{
if(data.Count!=0)
{
foreach (var item in data)
{
<tr>
<td>#item.Name</td>
</tr>
}
}
}
</tbody>
</table>
</div>
Thanks in advance.

How to add frame in razor view? Two webpages in single window

I am working on ASP.NET MVC4 project.
How can I add frame in razor-view? Actually, I want to add two frames in which one frame is having menu (all the links) and in second frame I have related WebPage(Controller/View).
Menu frame is fix and another frame will be changed according to menu selected using Ajax call.
It is good to use Partial View rather .
Here I am putting basic example of Partial View that I have used in my project.
Main View:
</fieldset>
<div>
<div style="width:30%; float:left; height:470px">
<table border="2" cellspacing="35">
<tr>
<td>
#Ajax.ActionLink("Member notificaiton", "MemberNotification", "Admin", new AjaxOptions { UpdateTargetId = "result" })
</td>
</tr>
</table>
</div>
<div id="result" style="width:70%; float:right; height:470px" >
</div>
</div>
}
</fieldset>
MemberNotification Controller:
public ActionResult MemberNotification()
{
return PartialView("MemberNotification", new MemberNotification());
}
And finally, generate your partial view for this MemberNotifcation Method.

MVC Html.ActionLink and Url.Action slow to render during Debug mode in Visual Studio

tl;dr Displaying a view in MVC with lots of records and 4 rendered links per record takes a long time to load while running in debug mode in VS2012.
I have an ASP.NET MVC4 web application and one of the views displays a table of 300 records. Whenever I run the application in debug mode in VS2012, it takes nearly 2 minutes to load this view. The table has 4 unique links being generated either by HTML.ActionLink or URL.Action. From testing, I know the generation of these links is the offending code.
Example of slow code:
<table>
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model) {
<tr>
<td>Bob</td>
<td>
Tony
<div style="margin-top: 5px;">
<i class="icon-edit"></i>
<i class="icon-file"></i>
<i class="icon-trash"></i>
</div>
<div style="margin-top: 5px;">
#Html.ActionLink("Do Something", "SomeAction", "SomeController", new { id = item.Id }, new { #class = "btn btn-success btn-mini", data_pe_type = "auto" })
</div>
</td>
</tr>
}
</tbody>
If I modify the code and build the base part of the links outside the foreach, the time to load reduces to 15 seconds.
Example of faster code:
<table>
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
#{
var editLink = Url.Action("Edit", "Home") + "/";
var detailsLink = Url.Action("Details", "Home") + "/";
var deleteLink = Url.Action("Delete", "Home") + "/";
var saLink = Url.Action("SomeAction", "SomeController") + "/";
}
#foreach (var item in Model) {
<tr>
<td>Bob</td>
<td>
Tony
<div style="margin-top: 5px;">
<i class="icon-edit"></i>
<i class="icon-file"></i>
<i class="icon-trash"></i>
</div>
<div style="margin-top: 5px;">
Do Something
</div>
</td>
</tr>
}
</tbody>
I'm using IIS Express and have tested this in both IE10 and Chrome 27 on Windows 7. In Chrome I have IPv6 disabled. This only happens during debug. I also had a co-worker test the same code on their machine and they encounter the same problem.
I've solved the performance problem by moving the base link generation outside the foreach, but now would like to know why Debugging caused my original code to render so slowly.
Caching is disabled with Debug=true. That will cause performance issues on many levels including route/action resolution, view/partial-view location resolution and parsing etc. Overall deploying application in Release mode (Debug=false) will improve performance up to 10 times, especially for large loops used in views.