Cascading Dropdown .NET CORE - ajax

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.

Related

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>
}

Ajax POST insert half data(some data is missed) Asp.Net MVC Ajax

I am inserting data via Ajax Post method. Data is inserted in one table but the data for secod table is not inserting. I have checked the code but I am not sure that what is mising in my code.
When I use the direct controller method then the data is inserted in both table but when I use the Ajax then data is inserted in only one table.
My Old working Controller code:
[HttpPost]
public ActionResult Create(StudentModel model)
{
if(ModelState.IsValid)
{
int id = stude.AddStudent(model);
if(id>0)
{
ModelState.Clear();
ViewBag.Success = "Data added Successfully";
}
}
return View();
}
My Ajax Controller Code:
[HttpPost]
public JsonResult Creates(StudentModel model)
{
if (ModelState.IsValid)
{
int id = stude.AddStudent(model);
if (id > 0)
{
ModelState.Clear();
ViewBag.Success = "Data added Successfully";
return Json("success", JsonRequestBehavior.AllowGet);
}
}
return Json("issue", JsonRequestBehavior.AllowGet);
}
My Model Code:
public int AddStudent(StudentModel stu)
{
student stud = new student()
{
FName = stu.FName,
LName = stu.LName,
Email = stu.Email
};
if (stu.address != null) {
stud.address= new address()
{
Details = stu.address.Details,
Country = stu.address.Country,
State = stu.address.State
};
}
using (var context = new StudentEntities())
{
context.students.Add(stud);
context.SaveChanges();
}
return stud.Id;
}
My Js/Ajax Code:
$(document).ready(function () {
//Add record
$("#add").click(function (e) {
e.preventDefault();
// var id = $();
var fname = $("#FName").val();
var lname = $("#LName").val();
var email = $("#Email").val();
var details = $("#Details").val();
var country = $("#Country").val();
var state = $("#State").val();
$.ajax({
async: true,
method: "POST",
url: '#Url.Action("Creates")',
data: JSON.stringify({
FName: fname, LName: lname, Email: email, Details: details,County: country, State: state
}),
dataType: 'JSON',
contentType: "application/json; charset=utf-8",
success: function (data) {
//window.location = data.newurl;
console.log(data);
},
error: function (err) {
alert('Failed to get data' + err);
}
});
return false;
});
});
Data is inserted in only student table and for the address table it returns null/empty and the data is skipped, although the same code will work if I remove the Ajax. But I want to use the Ajax so things will work smoothly.
Any help will be appreciated.
Update: Student Model class:
I am using N-Tire/3-Tire Architecture
My Student class Properties
public class StudentModel
{
public int Id { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public string Email { get; set; }
public Nullable<int> AddressId { get; set; }
public AddressModel address { get; set; }
}
My Address Class Properties
public class AddressModel
{
public int Id { get; set; }
public string Details { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
}
There were a lot of issues in my code. I am listing it below.
The main, which I think case the problem is the Id's of my textboxes, as I am using Razor Engine and it creates the id automatic, which cause the issue, So I fix it by adding the Id and Name manually, like
#Html.EditorFor(model => model.address.Details, new { htmlAttributes = new { #class = "form-control", Name="Details", Id="Details"} })
I have changed my ajax data code to:
FName: fname, LName: lname, Email: email, address: { Details: details, Country: country, State: state }
For success message I have change Return message in my controller like:
var scess = "Data added Successfully";
return Json(new { success = true, scess }, JsonRequestBehavior.AllowGet);
and in my view I have add this line in my ajax success call:
success: function (data) {
$("#msg").text(data.scess);
console.log(data);
},
Hope this will also help other users in future.

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.

Ajax paged list pass object to controller

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);
}
});

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