Click here to Skip to main content
14,391,100 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have an employee form with Id(Primary Key) column and Identity number column.
I am supposed to get the primary key from the first ten digits of the identity number, when my form saves the first ten digits must go to the primary key field.
My view is in html and not razor.

What I have tried:

I have tried using razor elements and adding a stored procedure to mysql but i cant call the stored procedure from my project.

Edit: (Details from solution #2)

I don't know how to implement the above on my current project.

this is my controller:
namespace MySqlCRUD.Controllers
{

    public class EmployeeController : Controller
    {
        private readonly AppDataContext context;

        public EmployeeController(AppDataContext context)
        {
            this.context = context;
        }

        [HttpGet]

        public IActionResult Index()
        {
            //var emps = context.Employees.ToList();

            // var emps = context.Employees
            // .FromSql("EXECUTE dbo.new_procedure")
            // .ToList();
            // return View(emps);

            using (var _context = new AppDataContext())
            {
                var data = context.Employees.FromSql($"SELECT IdentityNumber, substring(Id,1) from Hu.Employees").ToList();

                return View(data);
            }
        }

        [HttpGet]

        public IActionResult Create()
        {

            
           
            return View();
        }

        [HttpPost]

        public IActionResult Create(Employee model)
        {
            //SELECT IdentityNumber, substring(Id, 1) from Employees
            if (!ModelState.IsValid) return View(model);

            context.Add(model);
            ModelState.Clear();

            context.SaveChanges();
            ViewBag.Message = "Added succesfully";
            return View("create");

        }


my model:

[Key]
        public string Id { get; set; }

       // public string Id
       //  {
          //  get { return IdentityNumber != null ? IdentityNumber.Substring(10) : ""; }
       // }
        public string EmpName { get; set; }
     
        public string EmpSurname { get; set; }
        public string IdentityNumber { get; set; }

        [DataType(DataType.Date)]
        public DateTime? DateOfBirth { get; set; }

        public string Gender { get; set; }
        public string Race { get; set; }
        public string MaritalStatus { get; set; }

        public string ContactNumber { get; set; }
        public string EmailAddress { get; set; }
        public string PhysicalAddress { get; set; }
        public string PostalAddress { get; set; }

        public string Occupation { get; set; }
        public string Department { get; set; }
        public float AnnualIncome { get; set; }
        public float BasicSalary { get; set; }

        [DataType(DataType.Date)]

        public DateTime StartDate { get; set; }
        public string EducationLevel1 { get; set; }
        public string EducationLevel2 { get; set; }
        public string EducationLevel3 { get; set; }
        public string EducationLevel4 { get; set; }
        public string Education { get; set; }


    }



}


my view

@model MySqlCRUD.Models.Employee

<br/>
<style>
    .form-control {
        width: 200px !important;
    }
    .form-horizontal {
          margin: 5px 100px 100px 100px;
  

    }
    .btn-default {
        color: #0033cc;
        width: 80px;
        height: 30px;
        font-weight: bold;
    }

    .col-sm-2 {
        font-weight: bold;
    }

    .col-sm-3 {
        font-weight: bold;
    }

    h4{
    font-weight:bold;
    }

   .bdr{
       height:20px;

   }
   .bdr1{
       height:20px;

   }
   body{
                margin: 0;
                padding: 0;

   }
   
   
    nav{
        
      }

    h1{
        
    }
</style>
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">


</head>


<body>
    <nav id="con">
    <div class="container">
        <div class="container bdr1">
     

<form class="form-horizontal" asp-action="Create" asp-controller="Employee" method="post">
    <br/>
<br/>
    <div asp-validation-summary="ModelOnly"></div>
    <h1 >                        Add New Employee Details</h1>
    <br />
    <h4>
                                Personal Details              
                          
                                          
        Contact Details
    </h4>
   <div class="form-group " col-sm-3" style="display: flex">

        <input class="form-control col-sm-2" type="text"  value="Id" data-toggle="tooltip" data-placement="left" title="Name of employee" readonly>
        <div class="col-sm-3">
            <span asp-validation-for="Id" class="text-danger"></span>

        </div> <br/>

        <input class="form-control col-sm-2" type="text" value="Contact Number" data-toggle="tooltip" data-placement="left" title="Direct mobile line of the employee" readonly>
        <div class="col-sm-3">
            <input asp-for="ContactNumber" class="form-control" type="text" placeholder="Contact Number">
            <span asp-validation-for="ContactNumber" class="text-danger"></span>

        </div>
</div>
    <div class="form-group " col-sm-3" style="display: flex">

        <input class="form-control col-sm-2" type="text"  value="Name" data-toggle="tooltip" data-placement="left" title="Name of employee" readonly>
        <div class="col-sm-3">
            <input asp-for="EmpName" class="form-control" type="text" placeholder="Name" >
            <span asp-validation-for="EmpName" class="text-danger"></span>

        </div>
         <input class="form-control col-sm-2" type="text" value="Email Address" data-toggle="tooltip" data-placement="left" title="Personal email address of the employee" readonly>

        <div class="col-sm-3">
            <input asp-for="EmailAddress" class="form-control" placeholder="Email Address">
            <span asp-validation-for="EmailAddress" class="text-danger"></span>

        </div>
       
    </div>

    <div class="form-group " col-sm-3" style="display: flex">

        <input class="form-control col-sm-2" type="text" value="Surname" data-toggle="tooltip" data-placement="left" title="Surname of employee" readonly>
        <div class="col-sm-3">
            <input asp-for="EmpSurname" class="form-control" placeholder="Surname">
            <span asp-validation-for="EmpSurname" class="text-danger"></span>

        </div>
       <input class="form-control col-sm-2" value="Residential Address" data-toggle="tooltip" data-placement="left" title="Address whereby the employee stays" readonly>
        <div class="col-sm-3">
            <input asp-for="PhysicalAddress" class="form-control" placeholder="Residential Address">
            <span asp-validation-for="PhysicalAddress" class="text-danger"></span>

        </div>
    </div>
    <div class="form-group" col-sm-3" style="display: flex">
        <input class="form-control col-sm-2" value="Identity Number" data-toggle="tooltip" data-placement="left" title="Identification Number of the employee" readonly>
        <div class="col-sm-3">
            <input asp-for="IdentityNumber" class="form-control"  placeholder="Identity Number">
            <span asp-validation-for="IdentityNumber" class="text-danger"></span>

        </div>
         <input class="form-control col-sm-2" value="Postal Address" data-toggle="tooltip" data-placement="left" title="Address of the employee for sending mail" readonly>
        <div class="col-sm-3">
            <input asp-for="PostalAddress" class="form-control" placeholder="Postal Address">
            <span asp-validation-for="PostalAddress" class="text-danger"></span>

        </div>
    </div>


    <div class="form-group" col-sm-3" style="display: flex">
        <input class="form-control col-sm-2" value="Date of Birth" data-toggle="tooltip" data-placement="left" title="Birth date of the employee" readonly>
        <div class="col-sm-3">
            <input asp-for="DateOfBirth" class="form-control" placeholder="Date of Birth">
            <span asp-validation-for="DateOfBirth" class="text-danger"></span>

        </div>

       
    </div>

    <div class="form-group" col-sm-3" style="display: flex">
        <input class="form-control col-sm-2" value="Gender" data-toggle="tooltip" data-placement="left" title="Gender of the employee" readonly>
        <div class="col-sm-3">
            <select asp-for="Gender" class="form-control">
                <option value="0">Select</option>
                <option value="Female">Female</option>
                <option value="Male">Male</option>
                <option value="Other">Other</option>

            </select>
        </div>

    </div>


    <div class="form-group" col-sm-3" style="display: flex">
        <input class="form-control col-sm-2" value="Race" data-toggle="tooltip" data-placement="left" title="ethnicity of the employee" readonly>
        <div class="col-sm-3">
            <select asp-for="Race" class="form-control">
                <option value="0">Select</option>
                <option value="African">African</option>
                <option value="Caucasian">Caucasian</option>
                <option value="Indian">Indian</option>
                <option value="Chinese">Chinese</option>

            </select>
            <span asp-validation-for="Race" class="text-danger"></span>

        </div>

    </div>



    <div class="form-group" col-sm-3" style="display: flex">
        <input class="form-control col-sm-2" value="Marital Status" data-toggle="tooltip" data-placement="left" title="Description of a person's relationship" readonly>
        <div class="col-sm-3">
            <select asp-for="MaritalStatus" class="form-control">
                <option value="0">Select</option>
                <option value="Single">Single</option>
                <option value="Married">Married</option>
                <option value="Divorced">Divorced</option>

            </select>
            <span asp-validation-for="MaritalStatus" class="text-danger"></span>

        </div>

    </div>
    <br />
    <h4>
                         Occupational Details              
                          
                      
                          Education Details
    </h4>

    <div class="form-group " col-sm-3" style="display: flex">

        <input class="form-control col-sm-2" type="text" value="Occupation" data-toggle="tooltip" data-placement="left" title="A job or profession of the employee" readonly>
        <div class="col-sm-3">
            <input asp-for="Occupation" class="form-control" type="text" placeholder="Occupation">
            <span asp-validation-for="Occupation" class="text-danger"></span>

        </div>

        <input class="form-control col-sm-2" type="text" value="Qualification" data-toggle="tooltip" data-placement="left" title="A completed course" readonly>
        <div class="col-sm-3">
            <input asp-for="EducationLevel1" class="form-control" type="text" placeholder="Qualification">
            <span asp-validation-for="EducationLevel1" class="text-danger"></span>

        </div>
    </div>

    <div class="form-group " col-sm-3" style="display: flex">

        <input class="form-control col-sm-2" type="text" value="Department" data-toggle="tooltip" data-placement="left" title="A division of an area of specialisation " readonly>
        <div class="col-sm-3">
            <input asp-for="Department" class="form-control" placeholder="Department">
            <span asp-validation-for="Department" class="text-danger"></span>

        </div>
        <input class="form-control col-sm-2" type="text" value="Qualification" data-toggle="tooltip" data-placement="left" title="A completed course" readonly>

        <div class="col-sm-3">
            <input asp-for="EducationLevel2" class="form-control" placeholder="Qualification">
            <span asp-validation-for="EducationLevel2" class="text-danger"></span>

        </div>
    </div>

    <div class="form-group " col-sm-3" style="display: flex">

        <input class="form-control col-sm-2" type="text" value="Basic Salary" data-toggle="tooltip" data-placement="left" title="Salary paid to the employee each month" readonly>
        <div class="col-sm-3">
            <input asp-for="BasicSalary" class="form-control" type="text" placeholder="BasicSalary">
            <span asp-validation-for="BasicSalary" class="text-danger"></span>

        </div>

        <input class="form-control col-sm-2" type="text" value="Qualification" data-toggle="tooltip" data-placement="left" title="A completed course" readonly>
        <div class="col-sm-3">
            <input asp-for="EducationLevel3" class="form-control" type="text" placeholder="Qualification">
            <span asp-validation-for="EducationLevel3" class="text-danger"></span>

        </div>
    </div>

    <div class="form-group " col-sm-3" style="display: flex">

        <input class="form-control col-sm-2" type="text" value="Annual Income" data-toggle="tooltip" data-placement="left" title="The amount of income in a year" readonly>
        <div class="col-sm-3">
            <input asp-for="AnnualIncome" class="form-control" placeholder="Annual Income">
            <span asp-validation-for="AnnualIncome" class="text-danger"></span>

        </div>
        <input class="form-control col-sm-2" type="text" value="Qualification" data-toggle="tooltip" data-placement="left" title="A completed course" readonly>

        <div class="col-sm-3">
            <input asp-for="EducationLevel4" class="form-control" placeholder="Qualification">
            <span asp-validation-for="EducationLevel4" class="text-danger"></span>

        </div>
    </div>

    <div class="form-group " col-sm-3" style="display: flex">

        <input class="form-control col-sm-2" type="text" id="id1" value="Start Date" data-toggle="tooltip" data-placement="left" title="Date in which employee started or is to start working" readonly>
        <div class="col-sm-3">
            <input asp-for="StartDate" class="form-control" type="date" placeholder="Start Date">
            <span asp-validation-for="StartDate" class="text-danger"></span>

        </div>

    </div>
    <br />
    <div class="form-inline">

        <input type="submit" value="Save" class="btn btn-default" title="Add Emplyee" data-toggle="tooltip" onclick="getIdentityNumber()"/>
        <input type="button" value="Back" class="btn btn-default" onclick="history.go(-1)" />
        <input type="reset" value="Clear" class="btn btn-default" />
        <input type="button" value="Home" class="btn btn-default" onclick="location.href='@Url.Action("Index", "Home")'" />
    </div>

</form>
        </div>
        </div>
        </nav>
    </body>
</html>

@if (ViewBag.Message != null)
{
    <script type="text/javascript">
        window.onload = function () {
          alert ("@ViewBag.Message");
        };

    </script>
}

<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.3.1.min.js"

        asp-fallback-src="~/lib/jquery/dist/jquery.min.js"

        asp-fallback-test="window.jQuery"

        crossorigin="anonymous"

        integrity="sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT"></script>

<script type="text/javascript">
    $(document).ready(function (){
      var IdentityNumber = "1234567890123";
        var Id = IdentityNumber.substring(1,10);
    });

</script>
<script type="text/javascript">
    function getIdentityNumber(){
    var identitynumber = document.getElementById("IdentityNumber").value;
var id = identitynumber.substring(0,9);       
        
        document.getElementById("Id").value = id;
        };
</script>


the script is not working but displaying guid characters in mysqlworkbench when it should display substrings.
Posted
Updated 18-Oct-19 5:38am
v3
Comments
ZurdoDev 17-Oct-19 10:12am
   
How can we help?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Use the below snippets accordingly to get the 10 digit primary key, and assign it to your primary key field.

db.SaveChanges() //And use in your code for saving the data.


db.Id = tenDigitSubStringFunction(txtID.Text or your identity number) // txtID is you text box 
//tenDigitSubStringFunction will help perform your substring operation, add the below snippet code in this function.


Or else add your code here so that i can help you in fixing that.

Using JavaScript :
var str = "1234567890";
var res = str.substring(0, 9)

Using C# code
string name = "qwertyuiopasdfghjklzxcvbnmjk";
            Console.WriteLine(name.Substring(0, 9));
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100