Ajax paged list pass object to controller - asp.net-mvc-3

I am working on some advanced searching functionality and I have run into a roadblock. I would like to use AJAX for my search results and use the PagedList additons to handle paging. I have an AdvancedSearchResults action which takes a search filters class and the current page. The issue is how can I pass the search Filters class into this action correctly. I am sure something I am doing just isnt organized correctly as I would think this would be fairly common. I would really appreciate any help. Thanks in advance.
Controller Action:
public ActionResult AdvancedSearchResults(AdvancedSearchFilters searchFilters, int ? page)
{
//DO STUFF
return PartialView("_SearchResults", results);
}
AdvancedSearchResults Filters:
public class AdvancedSearchFilters
{
public string SearchText { get; set; }
public List<string> SelectedTableTypes { get; set; }
public List<Guid> SelectedGenreIds { get; set; }
public List<Guid> SelectedPlatformIds { get; set; }
public int YearMax { get; set; }
public int YearMin { get; set; }
public int RatingMin { get; set; }
public int RatingMax { get; set; }
}
Search Results Partial View:
#model List<SearchResultItem>
#using PagedList
#using PagedList.Mvc
<!-- 15 Per Page -->
<h2>Search Results</h2>
#if (ViewBag.OnePageOfSearchItems.Count > 0)
{
<div class="pagination">
#Html.PagedListPager((IPagedList)ViewBag.OnePageOfSearchItems, page => Url.Action("AdvancedSearchResults", "Search", new { searchFilters = ViewBag.AdvancedSearchFilters, page = page }),
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.OnlyShowFivePagesAtATime, "#main-results"))
</div>
foreach (var searchResultItem in Model)
{
<a href="#Url.Action(searchResultItem.ResultType, searchResultItem.ResultType, new { id = searchResultItem.ResultId })" class="result">
#if (searchResultItem.ProfileImageLocation != null)
{
<img src="#searchResultItem.ProfileImageLocation" alt="#searchResultItem.ResultName" />
}
<div class="result-info">
<h3>#searchResultItem.ResultName</h3>
<p>#searchResultItem.DisplayText</p>
</div>
</a>
}
<div class="pagination">
#Html.PagedListPager((IPagedList)ViewBag.OnePageOfSearchItems, page => Url.Action("AdvancedSearchResults", "Search", new { searchFilters = ViewBag.AdvancedSearchFilters , page = page }),
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.OnlyShowFivePagesAtATime, "#main-results"))
</div>
}
else
{
<span>No Matches.</span>
}

Try passing the data to action method in the form of json. Try the below code which pass the json object to action method.
//// construct your json object.
var jsonObj = '{"SelectedTableTypes" : [{"1","2"}]}';
var postData = $.parseJSON(jsonObj);
$.ajax(
{
url: // your URL with Action name AdvancedSearchResults,
data: postData,
cache: false,
dataType: "json",
success: function (result) {
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});

Related

Cascading Dropdown .NET CORE

I have a problem when Edit form with cascade dropdown but, on Create form don't have issue, data can get from database,
I'm new on .NET Core, please explain the problem.
Here My view Create Form
Create form
When I want to edit the data cant not retrieve from Database.
Here My View On Edit Form
Edit Form
Here my Ajax
public IActionResult AjaxMethod([FromBody] AutoDetails auto)
{
List<SelectListItem> items = new List<SelectListItem>();
switch (auto.type)
{
default:
foreach (var status in this._context.EmployeeStatus)
{
items.Add(new SelectListItem { Text = status.Name, Value = status.Id.ToString() });
}
break;
case "Status":
var employee = (from employees in this._context.Employees
where employees.employeeStatus.Id == auto.value
select employees).ToList();
foreach (var data in employee)
{
items.Add(new SelectListItem { Text = data.Name, Value = data.Name.ToString() });
}
break;
}
return Json(items);
}
Controller :
public async Task<IActionResult> Edit(int? id)
{
ViewData["team"] = _context.Teams.OrderBy(e => e.Name).ToList();
var result = await _context.ActionItems.FindAsync(id);
return View(result);
}
Here Js on Edit view:
<script type="text/javascript">
$(function () {
AjaxCall({ "type": '' }, 'Status');
$('#ddlstatus').on('change', function () {
var myData = {
"type": 'Status',
"value": $(this).find('option:selected').val()
};
AjaxCall(myData, 'Employee');
});
});
function AjaxCall(myData, element) {
$.ajax({
url: "/ActionItems/AjaxMethod/",
data: JSON.stringify(myData),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
switch (element) {
case "Status":
$('#ddlstatus').append($("<option></option>").val("0").html("--Select Status--"));
$.each(data, function () {
$('#ddlstatus').append($("<option></option>").val(this.value).html(this.text));
});
$('#ddlEmp').attr("disabled", "disabled");
break;
case "Employee":
if (data.length > 0) {
$('#ddlEmp').empty();
$('#ddlEmp').append($("<option></option>").val("0").html("--Select Name--"));
$.each(data, function () {
$('#ddlEmp').append($("<option></option>").val(this.value).html(this.text));
});
$('#ddlEmp').prop("disabled", false);;
break;
} else {
$('#ddlEmp').empty();
$('#ddlEmp').append($("<option></option>").val("0").html("--Select Name--"));
$('#ddlEmp').attr("disabled", "disabled");
}
}
},
});
}</script>
Here My Edit View Code :
<div class="row my-3">
<div class="form-group col-5">
<label>PIC Status</label>
<select class="form-control" id="ddlstatus" asp-for="EmployeeStatus">
</select>
</div>
<div class="form-group col-5">
<label>PIC Name</label>
<select class="form-control" id="ddlEmp" asp-for="Employee">
</select>
</div>
</div>
My model :
public int ActionId { get; set; }
public string ActionName { get; set; }
public string ActionTask { get; set; }
public string? ActionTaskUpdate { get; set; }
public string? Employee { get; set; }
public string? EmployeeStatus { get; set; }
public string? Team { get; set; }
public string ActionStatus { get; set; }
public string ActionCategory { get; set; }
public string CreateBy { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string? CreateDate { get; set; }
public string? UpdateDate { get; set; }
When I want to Edit Form, the value cant retrieve data.
Thanks a lot.

ASP.Net core - on form submit, refresh just a div and not the whole page

There are a lot of suggestions out there for this. But every answer I've seen hasn't really applied to .Net Core. I've been stuck for days so I'm hoping for help.
I have a very simple form - just two fields. When I submit, the controller returns to reload the page - return RedirectToAction(nameof(Index));.
I need it to just reload a div and not the whole Index page. I already have ajax in place to refresh the div on intervals. So, I need to figure out how to either return to just a div refresh and not a return RedirectToAction(nameof(Index)); - which I don't think is possible - or make it fire off that Ajax section on submit. I'm assuming Ajax the best option but I'm stumped.
Form:
<form asp-controller="Home" asp-action="CreateBreak" class="DthdropdownGroup ml-3">
<div class="btn-group" style="margin-left:-1rem">
<select class="form-group dropdownStyling btn-sm dropdown-toggle d-inline-block btn-outline-light" style="width: 7.4em;" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="EmployeeId" asp-for="EmployeeId">
<option>Dispatcher</option>
#foreach (var item in Model.Employees)
{
<option class="dropdown-item pr-1 form-control" value="#item.Id">#item.DisplayName</option>
}
</select>
</div>
<div class="btn-group">
<select class="form-group dropdownStyling btn-sm dropdown-toggle d-inline-block btn-outline-light" style="width: 7.3em" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="EmpPosition" asp-for="EmpPosition">
<option>Position</option>
#foreach (var item in Model.Positions)
{
<option class="dropdown-item pr-1" value="#item.Id">#item.PositionName</option>
}
</select>
</div>
<button type="submit" class="btn btn-sm btn-block dropdownStyling" id="break-submit" style="margin-left: -1rem; width:15rem;">Submit</button>
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateBreak([Bind("Id,TimeEntered,TimeCleared,EmpSent,EmployeeId,EmpPosition,RlfPosition")] Break #break)
{
if (ModelState.IsValid)
{
#break.TimeEntered = DateTime.Now;
_context.Add(#break);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(#break);
}
Ajax set up that already works to refresh just the sections I need refreshed:
function RefreshDropDownsPartial() {
$.ajax({
success: function () {
$(" #DropDownsPartialDiv ").load(window.location.href + " #DropDownsPartialDiv ");
$(" .audioAlert ").load(window.location.href + " .audioAlert ");
},
error: function () {
}
});
Part of the index page that establishes the div I need reset on form submit:
<div class="listContent d-inline-block" id="DropDownsPartialDiv">
#await Html.PartialAsync("../Shared/_Lists")
</div>
Break Model:
namespace Seating.Models
{
public partial class Break
{
public int Id { get; set; }
public DateTime TimeEntered { get; set; }
public DateTime? TimeCleared { get; set; }
public bool? EmpSent { get; set; }
public int EmployeeId { get; set; }
public int EmpPosition { get; set; }
public int? RlfPosition { get; set; }
public virtual Position EmpPositionNavigation { get; set; }
public virtual Employee Employee { get; set; }
public virtual Position RlfPositionNavigation { get; set; }
}
}
ViewModel that fills the dropdowns:
public class ListsVM
{
public ListsVM()
{
Employees = new List<Employee>();
Positions = new List<Position>();
}
public IEnumerable<Dth> Dths { get; set; }
public IEnumerable<Break> Breaks { get; set; }
public IEnumerable<Lunch> Lunches { get; set; }
public IEnumerable<Employee> Employees { get; set; }
public IEnumerable<Position> Positions { get; set; }
public int EmployeeId { get; set; }
public int EmpPosition { get; set; }
public bool EmpSent { get; set; }
public bool LunchTime { get; set; }
public bool LongerLunch { get; set; }
public bool DblLunch { get; set; }
}
The /Shared/_Lists just returns tables that go above the form. I can post that code but it's working well. I just need to refresh those tables on submit. Thank you all for your time.
You're on the right path with using ajax, follow steps below;
Use controller action below; we're now returning json, and binding only 2 parameters.
[HttpPost]
public async Task<JsonResult> CreateBreak(int EmployeeId, int EmpPosition)
{
Break newBreak = new Break();
newBreak.EmployeeId = EmployeeId;
newBreak.EmpPosition = EmpPosition;
newBreak.TimeEntered = DateTime.Now;
_context.Add(newBreak);
await _context.SaveChangesAsync();
return Json(newBreak);
}
Add an id to your form, I used ajax_submit.
<form id="ajax_submit" asp-controller="Home" asp-action="CreateBreak" class="DthdropdownGroup ml-3">
...
</form>
Add this new script, this will send the values to the controller action above. Once successful, it will refresh that part of the div.
#section scripts {
<script>
$(document).ready(function(){
// bind event to your form
$("#ajax_submit").submit(function(e){
// prevent regular form submit
e.preventDefault();
var data = {
'EmployeeId' : $("#EmployeeId").val(),
'EmpPosition' : $("#EmpPosition").val()
}
$.ajax({
url: '#Url.Action("CreateBreak","Home")',
type: 'POST',
data: data,
success: function(result){
console.log(result);
// refresh
$(" #DropDownsPartialDiv ").load(window.location.href + " #DropDownsPartialDiv ");
$(" .audioAlert ").load(window.location.href + " .audioAlert ");
},
error: function (err){
console.log(err);
}
});
})
});
</script>
}
3A. Check your layout;
Jquery should be before the RenderSection scripts
<script src="~/lib/jquery/dist/jquery.min.js"></script>
#RenderSection("Scripts", required: false)
You could also try $(document).on("submit","#ajax_submit", function(e)
#section scripts {
<script>
$(document).ready(function(){
// bind event to your form
$(document).on("submit","#ajax_submit", function(e){
// prevent regular form submit
e.preventDefault();
var data = {
'EmployeeId' : $("#EmployeeId").val(),
'EmpPosition' : $("#EmpPosition").val()
};
$.ajax({
url: '#Url.Action("CreateBreak","Home")',
type: 'POST',
data: data,
success: function(result){
console.log(result);
// refresh
$(" #DropDownsPartialDiv ").load(window.location.href + " #DropDownsPartialDiv ");
$(" .audioAlert ").load(window.location.href + " .audioAlert ");
},
error: function (err){
console.log(err);
}
});
});
});
</script>
}

How do I populate a drop-down list with List<string> using knockout.JS

I'm playing around with knockoutJS and am struggling to get a drop-down populated with data from my model.
I'm guessing I need to convert the model data to a JSON first but for the life of me can't figure it out.
I have a basic class:
public class QuoteViewModel
{
public QuoteViewModel()
{
QuoteLines = new List<QuoteLine>();
}
public int QuoteHeaderId { get; set; }
public string QuoteNumber { get; set; }
public string Status { get; set; }
public DateTime DateCreated { get; set; }
[DisplayName("Customer")]
public SalesCustomer SelectedCustomer { get; set; }
public List<string> Customers { get; set; }
public List<QuoteLine> QuoteLines { get; set; }
}
I have an ActionMethod in a controller:
[HttpGet]
public ActionResult CreateQuote()
{
QuoteViewModel quote = new QuoteViewModel();
quote.QuoteNumber = "";
quote.Status = "P";
quote.Customers = _dbContext.SalesCustomers.Select(x => x.CustomerName).ToList();
quote.QuoteLines = new List<QuoteLine>();
return View(quote);
}
And the View():
Razor:
<select class="form-control" id="SelectedCustomer" data-bind="options: availableCustomers, optionsText: 'CustomerName', value: SelectedCustomer, optionsCaption: 'Choose...'"></select>
ViewModel:
function QuoteViewModel() {
var self = this;
self.Id = ko.observable('#Model.QuoteHeaderId');
self.QuoteNumber = ko.observable();
self.Status = ko.observable('#Model.Status');
self.DateCreated = ko.observable();
self.availableCustomers = JSON.parse('#Html.Raw(Model.Customers)');
#*$.ajax({
type: "GET",
url: '#Url.Action("GetCustomers", "Test")',
success: function (data) {
$.each(data, function (index, value) {
self.availableCustomers.push(new Customer(value));
});
},
error: function (err) {
console.log(err.responseText);
}
});*#
self.SelectedCustomer = ko.observable();
self.QuoteLines = ko.observableArray([]);
self.AddQuoteLine = function (sku, description, bomDetails) {
self.QuoteLines.push(new QuoteLineViewModel(sku, description, bomDetails));
}
self.SaveToDatabase = function () {
var dataToSend = ko.mapping.toJSON(self);
$.ajax({
type: "POST",
url: '#Url.Action("CreateQuote", "Test")',
contentType: 'application/json',
data: dataToSend,
success: function (data) {
},
error: function (err) {
console.log(err.responseText);
}
});
}
The commented out code uses ajax to get the customers and push it onto the array and that works, but is there not a way to do it directly from the model data?
Edit 1:
I don't need access to the whole SalesCustomer object, just the string name:
If I change my code as such:
Controller:
quote.Customers = _dbContext.SalesCustomers.Select(x => x.CustomerName.Trim()).ToList();
Model class property:
public List<string> Customers { get; set; }
Razor:
<select class="form-control" id="SelectedCustomer" data-bind="options: availableCustomers, value: SelectedCustomer, optionsCaption: 'Choose...'"></select>
Javascript:
self.availableCustomers = ko.observableArray([]);
var customers = '#Html.Raw(JsonConvert.SerializeObject(Model.Customers))';
self.availableCustomers = JSON.parse(customers);
self.SelectedCustomer = ko.observable();
Now the drop-down is populated with the string values of customer names. However the selected customer is not passed back to controller on POST?
Edit 2:
Well I'm not sure why its working now, but it is.
The code is the same as in Edit 1.
The only thing I can think of is I was that I erroneously had the SelectedCustomer property type still set to SalesCustomer which will obviously not work:
public SalesCustomer SelectedCustomer { get; set; }
when it should be:
public string SelectedCustomer { get; set; }
Thank you user3297291 for the assistance in pointing me in right direction.

Custom Pagination With Last and Previous

I am working on an ecommerce site where I have to use custom pagination with Ajax and it's almost done. Here is an image that how it looks:
Now the issue - The red highlighted 5 is the last page and I am trying to show as follows:
I am not sure how to handle this but I've tried the following: (Almost all works except the last section)
#for (int i = 1; i <= Model.TotalPages; i++)
{
if (i == Model.CurrentPage)
{
<li class="#(i == Model.CurrentPage ? "active" : "")">#i</li>
}
else
{
<li><a class="page-number" href="javascript:void();">#i</a></li>
}
}
#if (Model.CurrentPage < Model.TotalPages)
{
<li>
<a class="last-page" href="javascript:void();">#Model.TotalPages</a> #*Here I am willing to show Last, but currently it shows the last page number 5*#
</li>
}
I am wondering how to show the name Last instead of the number I mean last page number. Again using partial view to show the details and pagination as follows:
#model OnlinePharmacy.Helpers.PagedData<OnlinePharmacy.Models.Product>
#{
ViewBag.Title = "Products";
List<OnlinePharmacy.Models.Product> products = ViewBag.Product;
}
<script src="~/Scripts/jquery-1.5.1.min.js"></script>
<script type="text/javascript">
$().ready(function () {
$(".page-number").live("click", function () { //This section deals the pagination
var page = parseInt($(this).html());
var url = '/Product/Products/';
url += 'page/' + page;
$.ajax({
url: '#Url.Action("Index")',
data: { "page": page },
success: function (data) {
$("#product-list").html(data);
}
});
window.history.pushState(
null,
'Products', // new page title
url // new url
)
});
});
$().ready(function () { //This section deals the pagination for the last page
$(".last-page").live("click", function () {
var page = parseInt($(this).html());
var url = '/Product/Products/';
url += 'page/' + page;
$.ajax({
url: '#Url.Action("Index")',
data: { "page": page },
success: function (data) {
$("#product-list").html(data);
}
});
window.history.pushState(
null,
'Products',
url
)
});
});
</script>
<div id="product-list">
#Html.Partial("Index")
</div>
I am sharing the models here for the products and pagination:
public class Product
{
public int ProductId { get; set; }
public int CategoryId { get; set; }
public string ProductName { get; set; }
[AllowHtml]
public string cktext { get; set; }
public double Price { get; set; }
public double Stock { get; set; }
public int Status { get; set; }
public IEnumerable<SelectListItem> Categories { get; set; }
}
public class PagedData<T> where T : class
{
public IEnumerable<T> Data { get; set; }
public int TotalPages { get; set; }
public int CurrentPage { get; set; }
}
With the following action, I am getting the page no. using Ajax (Please see the Ajax section above):
public ActionResult Index(int page)
{
List<Product> products = aProductManager.GetAllProducts();
ViewBag.Product = products.OrderBy(p => p.ProductId).Skip(PageSize * (page - 1)).Take(PageSize).ToList();
var dataFromDb = new PagedData<Product>();
dataFromDb.Data = products.OrderBy(p => p.ProductId).Skip(PageSize * (page - 1)).Take(PageSize).ToList();
dataFromDb.TotalPages = Convert.ToInt32(Math.Ceiling((double)products.Count() / PageSize));
dataFromDb.CurrentPage = page;
return PartialView(dataFromDb);
}
Updated: I require to do the following (Requirement little bit changed) - Frankly speaking, I am not aware how to do the below but would appreciate valuable suggestion. Thanks.
If you need to change your text you should change last section like this:
#if (Model.CurrentPage < Model.TotalPages)
{
<li>
<a class="last-page" href="javascript:void();">Last</a>
</li>
}
To make your solution work fine i suggest you to move actual page number to data attribute of your <a> tag like this:
#if (Model.CurrentPage < Model.TotalPages)
{
<li>
<a data-page-number="#Model.TotalPages" class="last-page" href="javascript:void();">Last</a>
</li>
}
And then change your js line:
$(".last-page").live("click", function () {
var page = parseInt($(this).html());
//your other code
});
To this:
$(".last-page").live("click", function () {
var page = parseInt($(this).data('page-number'));
//your other code
});
Ofcource it will be better to change your whole js solution to data attributes.

Cascading Dropdown in Mvc3

I am using Mvc3 I have 2 dropdown,BankBranch and city.
On first time load of view i am binding both the dropdon without cascading. then if user select city i want to change bankbranch according to that.
I am confused how i can achieve both the things together.
Thanks in advance.
This blog post should get you on your way. It provides examples for normal form posts, microsoft ajax form post, jquery ajax and others.
http://weblogs.asp.net/raduenuca/archive/2011/03/06/asp-net-mvc-cascading-dropdown-lists-tutorial-part-1-defining-the-problem-and-the-context.aspx
EDIT:
Generalized Code Explanation
Model
public class CascadeModel {
public SelectList<City> Cities { get; set; }
public SelectList<BankBranch> BankBranches { get; set;}
public int CityId { get; set; }
public int BranchId { get; set; }
}
public class Branch {
public int Id { get; set;}
public string Name { get; set; }
}
Controller:
public ActionResult BranchSelector() {
var viewData = new CascadeModel();
viewData.Cities = new SelectList(Repository.GetAllCities(), "Id", "Name", selectedCity);
viewData.BankBranches = new SelectList(Repository.GetBranchesByCity(selectedCity), "Id", "Name", "");
return View(viewData);
}
public JsonResult GetBranches(int id) {
return Json(Repository.GetBranchesByCity(id), JsonRequestBehavior.AllowGet);
}
View:
#model CascadeModel
#Html.DropDownListFor(m => m.CityId, Model.Cities, new { style = "width:250px" })
<br />
#Html.DropDownListFor(m => m.BranchId, Model.BankBranches, new { style = "width:250px" })
<script type="text/javascript">
$(document).ready(function() {
$("#CityId").bind('change', function() {
$.ajax({
url: '/Controller/GetBranches/' + $(this).val(),
success: function(data) {
//Clear the current branch ddl
//Load the new Branch data returned from the jquery call in the branches ddl
}
});
};
});
</script>

Resources