This my Fully working coder i have used in my project
In Controller Write this view and function:
public IActionResult UserLogin()
{
UserTable userTable = new UserTable();
// userTable.DepartmentList = new System.Web.Mvc.SelectList(PopulateDepartments(), "Key", "Value");
userTable.DepartmentList = PopulateDepartments();
return View(userTable);
}
private static List<MyListTable> PopulateDepartments()
{
List<MyListTable> items = new List<MyListTable>();
MySqlConnect connectOb = new MySqlConnect();
try
{
connectOb.conn.Open();
MySqlCommand command = new MySqlCommand("SELECT * FROM counter_table", connectOb.conn);
MySqlDataReader reader = command.ExecuteReader();
// Perform database operations
while (reader.Read())
{
items.Add(new MyListTable
{
Value = reader["counter_value"].ToString(),
Key = reader["Id"].ToString(),
});
// Debug.WriteLine("My Details: "+reader["Name"].ToString());
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
connectOb.conn.Close();
return items;
}
Create Class MyListTable.cs
namespace KioskCoreWebSystem.Models
{
public class MyListTable
{
public string? Key { get; set; }
public string? Value { get; set; }
}
}
Create Model UserTable.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.Mvc;
namespace KioskCoreWebSystem.Models
{
public partial class UserTable
{
public List<MyListTable>? DepartmentList
{
get;
set;
}
public string? DepartmentName { get; set; }
public string? DepartmentId { get; set; }
public int? Id { get; set; }
public string? Password { get; set; }
public string? UserName { get; set; }
public string? Email { get; set; }
public string? Phone { get; set; }
public string? Role { get; set; }
public string? UserStatus { get; set; }
public string? LastName { get; set; }
public string? FirstName { get; set; }
public string? CurrentPassword { get; set; }
public string? NewPassword { get; set; }
public string? ConfirmPassword { get; set; }
}
}
Create a html file for view, here my filename UserLogin.cshtml
@model UserTable
@{
ViewBag.Title = "Login";
/* https://dev.to/skipperhoa/login-and-register-using-asp-net-mvc-5-3i0g */
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ENROLLMENT</title>
<script type="text/javascript">
/* $(document).ready(function () {
$.ajax({
type: 'POST',
URL: '/UserLogin/UserLogin',
data: data,
dataType: 'JSON',
success: function (data) {
datadata = data.status
if (status == "1") {
window.location.href = '@Url.Action("Login","Kiosk")';
}
}
});
});*/
</script>
</head>
<body>
<div class="row justify-content-md-center">
<div class="col-md-4">
<div class="card">
<div class="card-header">
Login
</div>
<div class="card-body">
@using (Html.BeginForm("UserLogin", "Kiosk", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="editor-label">
User Name
</div>
@Html.TextBoxFor(model => model.UserName, "", new { @class = "form-control", @placeholder = "User Name" })
</div>
<div class="form-group">
<div class="editor-label">
Deartment
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.DepartmentId, new SelectList(Model.DepartmentList,"Key","Value"), "-- Select a Department --")
@*@Html.EditorFor(model => model.DepartmentId*@
@Html.ValidationMessageFor(model => model.DepartmentId)
</div>
<!-- <select id="departmentsDropdown" name="departmentsDropdown"></select-->
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password)
@Html.PasswordFor(model => model.Password, new { @class = "form-control", @placeholder = "Password" })
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-primary" value="Login"/>
</div>
}
</div>
</div>
</div>
</div>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/Ajax/jquery.min.js"></script>
<script type="text/javascript">
//alert("786786");
function GetDepartmentName() {
$.ajax({
dataType: 'JSON',
type: "GET",
url: "/Kiosk/GetCounterWaitingDepartment",
data: "{}",
success: function (data) {
// alert(JSON.stringify(data["data"]));
var obj = data.data
var s = '<option value="-1">Please Select a Department</option>';
for (var i = 0; i < obj.length; i++) {
s += '<option value="' + obj[i].id + '">' + obj[i].counterValue + '</option>';
}
$("#departmentsDropdown").html(s);
}
} ) ;
}
GetDepartmentName()
</script>
</body>
</html>
Comments1
En of Love: This Is Love Story(2020)