Confirmation Boostrap modal posting - asp.net-core

I'm attempting to take a confirm event and turning it into a modal. Here is the original code:
#page
#model AllCustomerModel
#{
ViewData["Title"] = "AllCustomer";
}
<h2>All Customers</h2>
<p>
<a asp-page="Customer">Create New Customer</a>
</p>
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayName("Name")
</th>
<th>
#Html.DisplayName("Address")
</th>
<th>
#Html.DisplayName("Country")
</th>
<th>
#Html.DisplayName("City")
</th>
<th>
#Html.DisplayName("Phone")
</th>
<th>Edit | Delete</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.customerList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address)
</td>
<td>
#Html.DisplayFor(modelItem => item.Country)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
<td>
#Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
<a asp-page="./EditCustomer" asp-route-id="#item.CustomerID">Edit</a> |
<a asp-page="./AllCustomer" onclick="return confirm('Are you sure you want to delete this item?');" asp-page-handler="Delete" asp-route-id="#item.CustomerID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
Code Behind,
public ActionResult OnGetDelete(int? id)
{
if (id != null)
{
var data = _context.CustomerTB.Find(id);
_context.Remove(data);
_context.SaveChanges();
}
return RedirectToPage("AllCustomer");
}
Here is what I've tried/currently trying (for brevity sake, I haven't pasted the MVP)
<button type="button" class="btn btn-primary" data-toggle="modal" data-id="#item.CustomerID" data-target="#myModal">
Delete
</button>
<div class="modal fade" id="myModal">
<form id="myForm" method="post">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Delete Customer</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
Are you sure you want to delete this customer?
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-danger">Yes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
</div>
</div>
</div>
</form>
</div>
Code Behind,
[BindProperty]
public Customer Customer { get; set; }
public void OnPost()
{
Customer customer = Customer;
if (customer != null)
_context.Remove(_context.CustomerTB.Find(customer.CustomerID));
customerList = _context.CustomerTB.ToList();
}
It will hit the OnPost method but the customer object is empty. Is this a proper way to tackle this issue? What more do I need to add in order to pick up the object on the code behind?

You need to pass the selected customerId to modal(with a hidden type) by javascript when click the button,and then bind the Customer.CustomerId in page using asp-for tag helper.
For example:
<button type="button" class="btn btn-primary myButton" data-toggle="modal" data-id="#item.CustomerID" data-target="#myModal">
Delete
</button>
<div class="modal fade" id="myModal">
<form id="myForm" method="post">
<div class="modal-dialog">
<div class="modal-content">
//..
<div class="modal-body">
<input type="hidden" class="hiddenid" asp-for="Customer.CustomerID" />
Are you sure you want to delete this customer?
</div>
//..
</div>
</div>
</form>
</div>
#section Scripts{
<script>
$('.myButton').on('click', function (event) {
var passedID = $(this).data('id');
$(".modal-body .hiddenid").val(passedID);
});
</script>
}

Related

Master Detail not showing details table in modal popup - ASP.NET Core

I am doing CRUD operations on a Master Detail models Team and TeamMember using modal popup. I followed this post to perform the Create operation and it is ok, now i am doing the Detail operation:
TeamController:
public IActionResult Detail(int id)
{
Team team = _dbcontext.Team
.Include(a => a.TeamMembers)
.Where(e => e.TeamId == id).FirstOrDefault();
return PartialView("_DetailTeamPartialView", team);
}
Index.cshtml detail button:
<button class="btn btn-success" data-toggle="modal" data-target="#("#DetailTeam-"+item.TeamId)" data-url="#Url.Action($"Detail/{item.TeamId}")">Detail</button>
#await Html.PartialAsync("_DetailTeamPartialView", item)
_DetailTeamPartialView.cshtml:
#model Team
#{
ViewData["Title"] = "_DetailTeamPartialView";
}
<div class="modal fade" role="dialog" tabindex="-1" id="#("DetailTeam-"+Model.TeamId)" aria-labelledby="DetailTeamLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog modal-xl modal-dialog-scrollable" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Detail Team</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick="javascript:window.location.reload()">
<span aria-hidden="true">×</span>
</button>
</div>
<div>
<dl class="row">
<dt class="col-sm-2">
#Html.DisplayNameFor(model => model.TeamName)
</dt>
<dd class="col-sm-10">
#Html.DisplayFor(model => model.TeamName)
</dd>
.......
<dt class="col-sm-2">
#Html.DisplayNameFor(model => model.TeamMembers)
</dt>
<dd class="col-sm-10">
<table class="table table-bordered table-sm">
<thead>
<tr>
<th>Member name</th>
<th>Birth date</th>
</tr>
</thead>
<tbody>
#for (int i = 0; i < Model.TeamMembers.Count; i++)
{
<tr>
<td>
<input asp-for="#Model.TeamMembers[i].MemberName" class="form-control-plaintext" readonly />
</td>
<td>
<input asp-for="#Model.TeamMembers[i].BirthDate" class="form-control-plaintext" readonly />
</td>
</tr>
}
</tbody>
</table>
</dd>
</dl>
</div>
</div>
</div>
</div>
This code displays only team details and doesn't show the TeamMember details. Any help??
EDIT
these are the classes:
public class Team
{
[Key]
public int TeamId { get; set; }
[Required]
public string TeamName { get; set; }
public virtual List<TeamMember> TeamMembers { get; set; } = new List<TeamMember>();
}
public class TeamMember
{
[Key]
public int TeamMemberId { get; set; }
[Required]
public string MemberName{ get; set; }
[Required]
public DateTime BirthDate{ get; set; }
[ForeignKey("TeamId")]
public int TeamId { get; set; }
public virtual Team Team{ get; set; }
}
'Model.TeamMembers.Count' in '#for (int i = 0; i < Model.TeamMembers.Count; i++)' return 0 why??
Master Detail not showing details table in modal popup - ASP.NET Core
I have gone through your shared code snippet between the line altough, you haven't shared your index page's code that certainly playing important role here. Nontheless, I tried to visualize what you are trying to achieve.
Expected Behaviour:
From the shared code, I have managed to build above output therefore, the expected behaviour might be while clicking on details button on the team's list which is the index page should display the details list of all the team member of that particular team. If that is the business scenario, then I would say, your approach were wrong and you are doing mess of course here thus, follow the steps below which will help to resolve your issue.
Database:
Note: Models would be same as yours. So not repeating that part.
Controller:
public class TeamController : Controller
{
private readonly ApplicationDbContext _context;
private readonly IWebHostEnvironment _environment;
public TeamController(IWebHostEnvironment environment, ApplicationDbContext context)
{
_environment = environment;
_context = context;
}
public IActionResult Index()
{
var teamList = _context.Teams.Include(a => a.TeamMembers).ToList();
return View(teamList);
}
}
Note: You are doing the mess here as you are using another details action for the _DetailTeamPartialView which is complete Irelevant here. Only Index action is capable here what you are trying to achieve. Just need to use Include(a => a.TeamMembers) which the action.
View Of Index()
#model IEnumerable<DotNet6MVCWebApp.Models.Team>
#{
ViewData["Title"] = "Index";
}
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.TeamId)
</th>
<th>
#Html.DisplayNameFor(model => model.TeamName)
</th>
<th>
Member Details
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.TeamId)
</td>
<td>
#Html.DisplayFor(modelItem => item.TeamName)
</td>
<td>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#("#DetailTeam-"+item.TeamId)">Detail</button>
#await Html.PartialAsync("_DetailTeamPartialView", item)
</td>
</tr>
}
</tbody>
</table>
_DetailTeamPartialView
Though, your _DetailTeamPartialView is alrigh and no vital issue has not seen,
#model DotNet6MVCWebApp.Models.Team
#{
ViewData["Title"] = "_DetailTeamPartialView";
}
<div class="modal fade" role="dialog" tabindex="-1" id="#("DetailTeam-"+Model.TeamId)" aria-labelledby="DetailTeamLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog modal-xl modal-dialog-scrollable" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Detail Team</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick="javascript:window.location.reload()">
<span aria-hidden="true">×</span>
</button>
</div>
<div>
<dl class="row">
<div style="margin-left:10px">
<dt class="col-sm-2">
#Html.DisplayNameFor(model => model.TeamName)
</dt>
<dd class="col-sm-10">
#Html.DisplayFor(model => model.TeamName)
</dd>
.......
<dt class="col-sm-2">
#Html.DisplayNameFor(model => model.TeamMembers)
</dt>
<dd class="col-sm-10">
<table class="table table-bordered table-sm">
<thead>
<tr>
<th>Member name</th>
<th>Birth date</th>
</tr>
</thead>
<tbody>
#for (int i = 0; i < Model.TeamMembers.Count; i++)
{
<tr>
<td>
<input asp-for="#Model.TeamMembers[i].MemberName" class="form-control-plaintext" readonly />
</td>
<td>
<input asp-for="#Model.TeamMembers[i].BirthDate" class="form-control-plaintext" readonly />
</td>
</tr>
}
</tbody>
</table>
</dd>
</div>
</dl>
</div>
</div>
</div>
</div>
Output
Takeaways:
As you are using HTML Tag helper class that is #await Html.PartialAsync("_DetailTeamPartialView", item) so you don't need to use below Detail action on your controller. Index action would handled the _DetailTeamPartialView autometically.
public IActionResult Detail(int id)
{
Team team = _dbcontext.Team
.Include(a => a.TeamMembers)
.Where(e => e.TeamId == id).FirstOrDefault();
return PartialView("_DetailTeamPartialView", team);
}

ascending order of data from database by Id

I am trying to learn asp.net-core, and I am stuck on trying to retieve the data from database in ascending order of database Id, my code is below but not sure why its not doing it, it does acccessnding order by page but I want all the data in ascending order by database Id, any pointers wold be great...
#model IEnumerable<Citrus.Models.Customers>
#{
ViewData["Title"] = "Index";
Pager pager = new Pager();
int pageNo = 0;
if (ViewBag.Pager !=null)
{
pager = ViewBag.Pager;
pageNo = pager.CurrentPage;
}
}
<form asp-controller="Customers" asp-action="Index">
<p>
<div class="container body-content">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label class="control-label" for="searchString"></label>
<div class="input-group">
<input class="form-control" type="text" name="searchString" placeholder="Search by company name" />
<span class="input-group-btn" name="searchString"><button class="btn btn-default btn-primary mr-1"><i class="fas fa-search"></i> Search</button></span>
#*<span asp-validation-for="searchString"></span>*#
<a class="btn btn-primary" asp-action="Create"><i class="far fa-plus-square"></i> Customer</></a>
</div>
</div>
</div>
</div>
</div>
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Company)
</th>
<th>
#Html.DisplayNameFor(model => model.Title)
</th>
<th>
#Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
#Html.DisplayNameFor(model => model.Surname)
</th>
<th>
#Html.DisplayNameFor(model => model.Tel)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.OrderByDescending(i => i.Id))
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Company)
</td>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Surname)
</td>
<td>
#Html.DisplayFor(modelItem => item.Tel)
</td>
<td>
<a asp-action="Edit" asp-route-id="#item.Id"><i class="fas fa-edit"></i></a> |
<a asp-action="Details" asp-route-id="#item.Id"><i class="fas fa-info-circle"></i></a> |
<a asp-action="Delete" asp-route-id="#item.Id"><i class="fas fa-trash-alt"></i></a>
</td>
</tr>
}
</tbody>
</table>
<div class="container">
#if (pager.TotalPages > 0)
{
<ul class="pagination justify-content-end">
#if (pager.CurrentPage > 1)
{
<li class="page-item">
<a class="page-link" asp-controller="Customers" asp-action="Index" asp-route-pg="1">First</a>
</li>
<li>
<a class="page-link" asp-controller="Customers" asp-action="Index" asp-route-pg="#(pager.CurrentPage -1)">Previous</a>
</li>
}
#for (var pge = pager.StartPage; pge <= pager.EndPage; pge++)
{
<li class="page-item #(pge == pager.CurrentPage ? "active" : "")">
<a class="page-link" asp-controller="Customers" asp-action="Index" asp-route-pg="#pge"> #pge</a>
</li>
}
#if (pager.CurrentPage < pager.TotalPages)
{
<li class="page-item">
<a class="page-link" asp-controller="Customers" asp-action="Index" asp-route-pg="#(pager.CurrentPage + 1)">Next</a>
</li>
<li>
<a class="page-link" asp-controller="Customers" asp-action="Index" asp-route-pg="#(pager.TotalPages)">Last</a>
</li>
}
</ul>
}
</div>
Many Thank
John
You need to use OrderBy in backend,use OrderBy firstly,and then get the current page data,for example:
List<Citrus.Models.Customers> data = _context.Customers
.OrderBy(c => c.Id)
.Skip(position).Take(pageSize)
.ToList();
For details,you can refer to the official doc.

How to replace in Laravel 8

You can see I can add numbers by doing CRUD operation in laravel
I have added 1.6 and it shows 1.6
But I want when I add 1.6 it will replace and will show one point six
This is NumberController.php
public function index()
{
$products = Product::latest()->paginate(5);
return view('products.index',compact('products'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'detail' => 'required',
]);
Product::create($request->all());
return redirect()->route('products.index')
->with('success','Product created successfully.');
}
I will add my value here
This is my create.blade.php
<form action="{{ route('products.store') }}" method="POST">
#csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Detail:</strong>
<input type="double" name="detail" class="form-control" placeholder="Detail">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
I have to show replace value here
This is my index.blade.php
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Name</th>
<th>Details</th>
<th width="280px">Action</th>
</tr>
#foreach ($products as $product)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->detail }}</td>
<td>
<form action="{{ route('products.destroy',$product->id) }}" method="POST">
<a class="btn btn-info" href="{{ route('products.show',$product->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
#endforeach
</table>

How to get the updated count of rows in a table after changing the page number

Actually in the table i'm working on there is pagination. I could get the count of rows present in the first page.
When i change the page number, table row count is not getting updated as per the second page.
How to force selenium to update the count after changing the page number. page refresh is not the solution as i need to navigate to second page again
public Boolean checkMessageIsFailed(String message, String channelNameToCheck, String channelTypeToCheck) {
UIPublish uiPublish = new UIPublish(driver);
Boolean isMessageSent = null;
SimplifyUtils.waitTillElementFound(uiPublish.currentPageNumber, 60);
//getting the page count
int pageCount = Integer.parseInt( uiPublish.totalPages.getText());
for (int j=1; j< pageCount;pageCount++)
{
//getting the rows present in the table
int messagesCount = uiPublish.listOfTextInMessages.size();
for (int i = 0; i < messagesCount; i++)
{
//getting the text from each row to match
//matching the text of the required message sent with the messages present in the sent messages
if (uiPublish.listOfTextInMessages.get(i).getText().equalsIgnoreCase(message))
{
String currentChannel = driver.findElements(By.xpath("//*[#id='inbox-wrapper']//tr["+ (i + 1)+ "]")).get(i).getAttribute("data-original-title");
}
}
//navigating to naxt page if the condition is not matched
uiPublish.navigateToNextPage.click();
}
return isMessageSent;
}
<div class="grid simple">
<div class="grid-body no-border email-body">
<br>
<div class="row-fluid">
<div id="email-list" class="table-responsive">
<form id="schMsgFORM" name="schMsgFORM" action="/viewSentSchMessage.action"
method="post">
<table class="table table-striped table-hover" id="emails">
<thead>
<tr>
<th>
</th>
<th>Social Channel
</th>
<th>Message
</th>
<th>Scheduled Time
</th>
<th>Actions
</th>
</tr>
</thead>
<tbody>
<tr id="outputWTFrUjUyYlE1d1piNG1XUmNuUFBnUT09">
<td class="small-cell v-align-middle" style="width: 2%">
<div class="checkbox check-success ">
<input name="msgcheckbox" id="chkboxWTFrUjUyYlE1d1piNG1XUmNuUFBnUT09"
value="WTFrUjUyYlE1d1piNG1XUmNuUFBnUT09" type="checkbox">
<label for="chkboxWTFrUjUyYlE1d1piNG1XUmNuUFBnUT09"></label>
</div>
</td>
<td style="width: 150px">
<div class="account-sent">
<div class="row">
<div class="display-inline">
<div class="profile-pic" data-toggle="tooltip" title=""
data-original-title="galloway360">
<img class="account-profile"
src="http://pbs.twimg.com/profile_images/378800000473105984/b0ad2b50b4fb81303d32720afea274ea_normal.png"
alt="">
<div class="social-icon-pub">
<span
class="social-icon-twitter img-responsive social-icon-box">
</span>
</div>
</div>
</div>
</div>
</div>
</td>
<td style="width: 50%">
<div class="muted message-wrap">
<br>
Source: Auto 2016/12/08 17:35:30 Message: How's your day?
</div>
<p class="sch-details">
Sent by: Nagarjuna reddy
<em>Scheduled from Profile :
NA
</em>
</p>
</td>
<td style="width: 15%">
<p class="muted m-t-5">
2016-12-08 05:35 PM
</p>
<p class="region">
(Asia/Calcutta)
</p>
</td>
<td style="width: 10%">
<a
href="/social/editUpdate?s360securetoken=SU0wxc4jrR8DE8Lf5hzKnRYasjg&schMsgId=WTFrUjUyYlE1d1piNG1XUmNuUFBnUT09&pageSource=publish"
data-toggle="tooltip" title="" type="button"
class="btn btn-success btn-small" data-original-title="Edit">
<i class="fa fa-pencil">
</i>
</a>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</div>
</div>

AngularJs in not working in Partial View

I am working in asp.net mvc4 with Razor View. I am using angularJs with it. But in partial view angularJs is not working.
Here is my view code:
#using (Html.BeginForm("Create", "Requisition", FormMethod.Post, new { id = "RequisitionForm", ng_app = "" }))
{
#Html.ValidationSummary(true)
<div class="divSection">
<div class="divBox divSectionLeftLeft">
<label>Product</label>
</div>
<div class="divBox divSectionLeftRight">
<input type="text" id="ProductName" name="ProductName" ng-model="newItem.ProductName" />
</div>
<div class="divBox divSectionRightLeft">
<label>Quantity</label>
</div>
<div class="divBox divSectionRightRight">
<input type="text" id="Quantity" name="Quantity" ng-model="newItem.Quantity" />
</div>
</div>
<div class="divClear">
</div>
<div class="ContainerRowButton">
<button type="button" class="btn btn-primary btn-mini" ng-click="items.push(newItem); newItem={}">Add Req-Detail</button>
</div>
<div style="width: 800px; height: 160px; overflow-y: scroll">
<table class="table table-striped table-bordered" id="RequisitionDetailsTbl" ng-init="items=[]">
<thead>
<tr>
<th>Product
</th>
<th>Quantity
</th>
<th>Action</th>
</tr>
</thead>
<tr ng-hide="items.length">
<td colspan="3">No Requisition Added Yet. . .</td>
</tr>
<tbody ng-repeat="item in items">
<tr>
<td>{{item.ProductName}}
</td>
<td>{{item.Quantity | number:2}}
</td>
<td>
<button type="button" id="delReqDetail" ng-click="items.splice($index, 1);" class="btn btn-primary btn-mini">Delete</button>
</tr>
</tbody>
</table>
</div>
}
My _Layout is like this:
#Styles.Render("~/Content/css")
#Scripts.Render("~/bundles/jquery, ~/bundles/jqueryui, ~/bundles/others")
Here "~/bundles/others" contains the angular.js.
I am not getting any error in firebug console. What wrong am I doing?
One more thing I am showing the partialview in popup window. The code Works fine if I don't use it in popup window.