I have a Razor page which contains a table and partial view with source as a List //(Model.LeaveTypeList)
#using (Html.BeginForm())
{
<table>
#*Grid Added as Partial View for leave Types*#
<tr>
<td colspan="2">
<div>
#Html.Partial("LeaveTypesPartial", Model.LeaveTypeList)
</div>
</td>
</tr>
</table>
}
This is the partial view (LeaveTypesPartial.cshtml)
#model List<AscoraWeb.Models.LeaveTypeModel>
#{
Layout = null;
}
#{
//canSort:false,
var grid = new WebGrid(source: Model,
defaultSort: "Name",
rowsPerPage: 7);
}
<table>
<tr>
<td colspan="2" style="text-align: left; font-family: Arial Baltic; font-style: italic;font-size: 15px">
These leaves can be assigned to Employees in the Service Calender for scheduling
</td>
<td colspan="2" style="text-align: left; font-family: Arial Baltic; font-style: italic;font-size: 15px">
</td>
</tr>
#*Row with buttons*#
<tr>
<td align="right" colspan="1">
<input type="button"/>
<input type="button"/>
</td>
</tr>
<tr>
<td>
<div id="grid">
#grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("name")
)
)
</div>
</td>
</tr>
</table>
In the application controller we are assigning some value to the partial view
public ActionResult RatesAndCharges()
{
//Message that will appear before the content of the view is shown
ViewBag.Message = "This is the Rates and Charges section!";
//Adding content for the leave types grid
var ratesChargesModel = new RatesChargesModel { };
ratesChargesModel.LeaveTypeList = new List<LeaveTypeModel>();
var leaveTypeModel = new LeaveTypeModel { Name = "Paid Leave" };
ratesChargesModel.LeaveTypeList.Add(leaveTypeModel);
leaveTypeModel = new LeaveTypeModel { Name = "Unpaid Leave" };
ratesChargesModel.LeaveTypeList.Add(leaveTypeModel);
return View(ratesChargesModel);
}
This works fine and I am able to see my Grid with elements
The grid elements can be sorted by Name (as we have defaultsort: Name)
but when we click on name the whole page gets refreshed (call being made to public ActionResult RatesAndCharges())
Hence I need a way to sort my elements in the grid without having to refresh my page.
You would have to make an AJAX request to the server using JQuery or something else... I don't know that the WebGrid supports this or not. But you can always do it yourself by using JQuery to invoke an action method, which returns a partial view, and you swap the old HTML for the new.
Related
I am writing a movie app that allows you to rent movies (similar to Redbox). I have a CheckOut cart view containing a table. Each table row has a remove button which uses AJAX to delete element in the view and also update the SQL database that the app works with. After removing any items from the cart, the user should be able to click 'purchase' and process the items that were left in the cart, all without needing to refresh the page.
I have an Order model containing a list of OrderDetails. Each OrderDetails item has information about a movie. It is data from OrderDetails list that the table is populated with.
The issue comes in when I remove items from cart and try to post the form with the values in the table. My CheckOut HttpPost controller method receives the model, but the OrderDetail list still has the item count it originally had before I removed items from cart. Logically, there is no data bound to the properties since I deleted the hidden tags I had in each record.
Because the list contains elements I don't need, processing the list results in garbage data going into the database.
I tried to simply remove the garbage elements within my CheckOut HttpPost method before it begins processing the list. This worked great but I don't want to have to remove anything in the CheckOut method after posting the form. I'm expecting the list to not contain the elements.
CheckOut POST method:
[HttpPost]
public IActionResult CheckOut(Order order)
{
if (ModelState.IsValid == false)
{
return View("CheckOut", order);
}
foreach (var orderDetailObj in order.OrderDetailsList)
{
_checkOutService.StoreMoviesInOrder(GetConnectionString(), order.OrderId, orderDetailObj);
}
return RedirectToAction("PurchaseSummary", new { Id = order.OrderId });
}
CheckOut.cshtml view:
#model MovieContract.Order
...
#for (int i = 0; i < Model.OrderDetailsList.Count; i++)
{
<tr>
<td>
<input type="button" name="btnRemove" class="removeButton" value="Remove" onclick="Remove(this, '#Model.CartId', #Model.OrderDetailsList[i].Movie.FilmId)" />
</td>
<td hidden>
<input asp-for="#Model.OrderDetailsList[i].Movie.AddedToCart" value="#Model.OrderDetailsList[i].Movie.AddedToCart" hidden />
</td>
<td hidden>
<input asp-for="#Model.OrderDetailsList[i].Movie.FilmId" value="#Model.OrderDetailsList[i].Movie.FilmId" hidden />
</td>
<td>
<input asp-for="#Model.OrderDetailsList[i].Movie.FilmName" value="#Model.OrderDetailsList[i].Movie.FilmName" hidden />
#Model.OrderDetailsList[i].Movie.FilmName
</td>
<td>
<input asp-for="#Model.OrderDetailsList[i].Movie.GenreName" value="#Model.OrderDetailsList[i].Movie.GenreName" hidden />
#Model.OrderDetailsList[i].Movie.GenreName
</td>
<td>
<input asp-for="#Model.OrderDetailsList[i].Movie.PricePerDay" value="#Model.OrderDetailsList[i].Movie.PricePerDay" class="pricePerDay" hidden />
#Html.DisplayFor(modelItem => #Model.OrderDetailsList[i].Movie.PricePerDay)
</td>
<td hidden>
<input asp-for="#Model.OrderDetailsList[i].Movie.AmountOnHand" value="#Model.OrderDetailsList[i].Movie.AmountOnHand" hidden />
</td>
</tr>
}
As for AJAX, I simply have an AJAX function that calls a post controller method. The method deletes the appropriate item from the database and returns NoContent();. Upon success, AJAX deletes the desired row from the view.
I expect that by the time I reach the CheckOut HttpPost method, the parameter object's list property will contain less elements if I had decided to remove any from the cart. I don't want to have to refresh the whole page to rebuild my model each time I remove an item from the cart.
Here is a working demo :
View
#model AjaxDeleteItem.Models.Order
<div>
<form method="post" asp-action="CheckOut">
<table class="table" id="table">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
#for (int i = 0; i < Model.OrderDetailsList.Count; i++)
{
<tr class="count">
<td>
<input type="button" name="btnRemove" class="removeButton" value="Remove" onclick="Remove(this, #Model.OrderDetailsList[i].Id)" />
</td>
<td >
<input class="FilmId" asp-for="#Model.OrderDetailsList[i].Movie.FilmId" value="#Model.OrderDetailsList[i].Movie.FilmId" />
</td>
<td >
<input class="FilmName" asp-for="#Model.OrderDetailsList[i].Movie.FilmName" value="#Model.OrderDetailsList[i].Movie.FilmName" />
</td >
<td>
<input class="GenreName" asp-for="#Model.OrderDetailsList[i].Movie.GenreName" value="#Model.OrderDetailsList[i].Movie.GenreName" />
</td>
<td>
<input class="PricePerDay" asp-for="#Model.OrderDetailsList[i].Movie.PricePerDay" value="#Model.OrderDetailsList[i].Movie.PricePerDay" />
</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Submit"/>
</form>
</div>
#section Scripts
{
<script>
function Remove(obj,id) {
$.ajax({
type: "post",
url: "/orders/deleteorderitem?id="+id,
success: function () {
$(obj).closest('tr').remove();
var count = $(" tbody tr").length;
var i = 0;
$("tbody tr").each(function () {
var row = $(this);
if (i < count)
{
row.find("input[class=FilmId]").attr("name", "OrderDetailsList[" + i + "].Movie.FilmId");
row.find("input[class=FilmName]").attr("name", "OrderDetailsList[" + i + "].Movie.FilmName");
row.find("input[class=GenreName]").attr("name", "OrderDetailsList[" + i + "].Movie.GenreName");
row.find("input[class=PricePerDay]").attr("name", "OrderDetailsList[" + i + "].Movie.PricePerDay");
i++;
}
});
},
error: function () {
alert("Fail to delete");
}
});
}
</script>
}
2.Controller:
[HttpPost]
public async Task<IActionResult> DeleteOrderItem(int id)
{
var orderdetail = await _context.OrderDetails.FindAsync(id);
_context.OrderDetails.Remove(orderdetail);
await _context.SaveChangesAsync();
return NoContent();
}
[HttpPost]
public IActionResult CheckOut(Order order)
{
if (ModelState.IsValid == false)
{
return View("Details", order.CartId);
}
//the stuff you want
}
3. Result :
I'm studing Asp.Net core web applications.
I successfully created a CRUD website.
In the site I want to create a link to an external URL, e.g., to www.google.com
Here, the link is created this way.
https://localhost:44311/pVagaDivulgar/details/www.google.com
expected: www.google.com
Curso
Go
I dont know if I have to set some properties to make it work.
#model SiteJob1.ViewModel.pVagaDivulgar.IndexViewModel
#{
ViewData["Title"] = "details";
}
<h1>Jobdetails</h1>
#foreach (var item in Model.VMvagasdivulgar)
{
<h4>#item.VagadivulgarVaga.VagaTitulo</h4>
}
<table class="table">
<thead>
<tr>
<th>ID</th><th>Course</th><th>Link</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.VMvagasdivulgar)
{
<tr>
<td>
#item.VagadivulgarId
</td>
<td>
#item.VagadivulgarCurso1Navigation.CursoNome
</td>
<td>
Curso
Go
</td>
</tr>
}
</tbody>
</table>
<div>
#Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
<a asp-action="Index">Back to List</a>
</div>
ok, i understand your problem if you need to open Url then write full url like https://toastguyz.com
public ActionResult Index()
{
ViewBag.Title = "Home Page";
ViewBag.link = "https://www.toastguyz.com";
return View();
}
Is it possible to combine these two html tables into one table instead?
I can't seems to do it no matter how, it either tell me no start tag or no close tag for the table.
Goals:
1) One <table> with two <tr> and each <tr> has two <td>.
2) Top download button (from top form) in top <tr>.
3) Bottom download button (from bottom form) in bottom <tr>.
<table>
<tr>
<td></td>
<td>
#using (Html.BeginForm("GenerateExcelReport", "Pages", FormMethod.Post, new { name = "formExcel", id = "formExcel" }))
{
#Html.DevExpress().Button(button =>
{
button.ClientSideEvents.Click = "OnClick";
button.Name = "buttonExcel";
button.Text = Resources.Global.buttonExportExcelExtra;
button.UseSubmitBehavior = true;
}).GetHtml()
}
</td>
</tr>
</table>
#using (Html.BeginForm("DownloadMainReport", "Pages", FormMethod.Post))
{
<table>
<tr>
<td style="padding:2px;">
#Html.DevExpress().ComboBox(settings =>
{
settings.Name = "ExportMode";
settings.Properties.Caption = Resources.Global.labelExportMode;// "Details Export Mode ";
settings.Properties.ValueType = typeof(GridViewDetailExportMode);
settings.SelectedIndex = 0;
}).BindList(Enum.GetValues(typeof(GridViewDetailExportMode))).Bind(GridViewDetailExportMode.None).GetHtml()
</td>
<td style="padding:2px;">
#Html.DevExpress().Button(settings =>
{
settings.Name = "buttonExportExcel";
settings.Text = Resources.Global.buttonExportExcel;
settings.UseSubmitBehavior = true;
}).GetHtml()
</td>
</tr>
</table>
#Html.Partial("HomeReportPartial", Model)
}
Even if I use div, it will tell me it cannot find matching tag.
<div class="container-fluid">
<div class="row">
<div class="col-md-1">1</div>
<div class="col-md-1">
#using (Html.BeginForm("test0", "test0", FormMethod.Post))
{ }
</div>
<div class="col-md-10">3</div>
</div>
#using (Html.BeginForm("test1", "test1", FormMethod.Post))
{
<div class="row">
<div class="col-md-1">1</div>
<div class="col-md-1">2</div>
<div class="col-md-10">3</div>
</div>
</div>
}
Parser Error Message: Encountered end tag "div" with no matching start tag. Are your start/end tags properly balanced?
I am using the spring MVC model attribute approach. When opening a screen i got to read a JSON string and create a map. Display the map in a drop down list. I could see the values in HTML source code but not seeing the drop down in the screen.
#RequestMapping("/xxx")
public ModelAndView saws(Map<String, Object> model1) throws IOException {
ModelAndView model = new ModelAndView("xxx");
model.addObject("message", this.welcome);
Map<String, String> offers = engine.getOffers();
model.addObject("offers", offers);//map of offers
model.addObject("inputs", new inputs());
return model;
}
JSP
<form:form action="getoffers" method="get" modelAttribute="inputs">
<body>
<table align="center">
<tr class="blank_row">
<td colspan="2"></td>
</tr>
<tr class="blank_row">
<td colspan="2"></td>
</tr>
<tr align="center">
<th span
style="color: red; font-weight: bold; word-wrap: break-word;"
align="center">Select the service to view current offers</th>
</tr>
<tr>
<td><form:select path="offers">
<form:options items="${offers}" />
</form:select></td>
</tr>
</table>
And in the screen i don't see the drop down.when i check the source code I see this. Pls see the pictures
Assume you already had the sping-mvc dependency.
The html source file is not supposed to have <form:...> prefix. It is not rendered correctly.
Put this in your jsp file:
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
I highly recommend that you use Thymeleaf template engine instead of JSP. The reasons are too long to fit in this answer area.
hey i have this view in my mvc application. If you look at the "tr" where it says "lönsamhet: " and then a method is executed which counts and returns the profilbility of the project. and if the profibility method returns a number bigger than 20% i want the table background color to turn green. and if less than 20% i want it to turn red... How can i do that? and my model is a list of a object i made called project. css is not my best side so would appricieate help. thanks.
#model List<Blogg.Models.BlogPost>
#foreach (var item in Model)
{
<div>
<table style="font-family: Arial; border:1px solid black; width: 1000px; height: 20px">
<tr>
<td><b>Projekt Numer: </b>#item.ID</td>
</tr>
<tr>
<td><b>Projekt Namn: </b>#item.name</td>
</tr>
<tr>
<td><b>Kund Namn: </b></td>
</tr>
<tr>
<td><b>Lönsamhet: </b>#item.profitability()</td>
</tr>
<tr>
<td><b>#ViewBag.Basecamp</b></td>
</tr>
<tr>
<td>#Html.ActionLink("Detaljer", "Details", new { id = item.ID })</td>
</tr>
</table>
</div>
Could you not use JQuery?
<td class="profit"><b>Lönsamhet: </b>#item.profitability()</td>
if(variableName > 20){
$('.profit').css("background-color","green");
}
else{
$('.profit').css("background-color","red");
}
For multiple fields do a jquery.each loop and grab the value of each class.
$('.profit').each(function(){
var variableName = $(this).val();
if(variableName > 20){
$('.profit').css("background-color","green");
}
else{
$('.profit').css("background-color","red");
}
});