Click here to Skip to main content
15,610,223 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a check box whose purpose is to replace values in text box if checked. Like if the check box is checked the residential address will be duplicated in the permanent address.

the problem is i can not pass the value of the checkbox to controller.
I need to pass the value of the checkbox to the controller and in controller i am trying to achieve some thing like this


public async Task<IActionResult> Edit(Somemodel sm)
{
   somemodel mdl =  new somemodel();
   if(isaddress ==  true)
{
 presentadd =  resadd;
}
else
{
presentadd = sm.address;
}

}


i am using .net 6 mvc with mysql

my db is like this
SELECT * FROM handbook.canditate_basic_information;CREATE TABLE `canditate_basic_information` (
  `Canditate_Id` int NOT NULL,
  `Canditate_Premanent_Address` varchar(45) NOT NULL,
  `Canditate_Residential_Address` varchar(45) DEFAULT NULL,
  `IsAddressSame` tinyint DEFAULT NULL,
  
  PRIMARY KEY (`Canditate_Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;


What I have tried:

this is how my model looks

public partial class BasicInformation
{
public sbyte? IsAddressSame { get; set; }  -- this is check box
public string peraddress { get; set; }
public string resaddress { get; set; }
}


this is my view

<pre>@model EmployeeJoiningPortal.Data.CanditateBasicInformation

@{
    ViewData["Title"] = "Edit";
    Layout = "~/Views/Shared/_UserLayout1.cshtml";
}

<!DOCTYPE html>
<html lang="en">
<head>
    <script>
	</script>
</head>
<body>
 <div class="row mb-2">
  <label class="col-sm-2 col-form-label">Permanent Address</label>

 <div class="col-sm-2">
 <input style="width: 500px; height: 80px;" id="permanentaddress" asp-for="CanditatePremanentAddress" type="text" class="form-control" required>
<span asp-validation-for="CanditatePremanentAddress" class="text-danger"></span>
</div>
</div>
<div class="col-sm-6"> <input type="checkbox"  id="chkaddress" value="1" name="filladdress">
<span asp-validation-for="IsAddressSame" style="font-family:Arial; font-size:20px;" id="chkaddress" class="text-danger">Residential Address Same as Permanent Address</span>
</div>
<div class="row mb-2"><label class="col-sm-2 col-form-label">Permanent Address</label>
<div class="col-sm-2">
<input style="width: 500px; height: 80px;" id="permanentaddress" asp-for="CanditatePremanentAddress" type="text" class="form-control" required>
<span asp-validation-for="CanditatePremanentAddress" class="text-danger"></span> </div>
		 <footer>
        <div></div>
    </footer>
</body>



</html>



i am new to mvc . please guide
Posted
Comments
CHill60 14-Dec-22 7:35am    
Why have IsAddressSame as a nullable sbyte? Why not a simple boolean?
Member 13998042 14-Dec-22 7:38am    
i am using datafirst approach so when i scaffold db to model in c# it automatically get's this datatype. also new to entity framework. Please suggest some guidence.
Dave Kreskowiak 14-Dec-22 8:01am    
You should NOT be using database model classes to pass data between the view and controller. Those should be different view model classes specifically written for the purpose.
PIEBALDconsult 14-Dec-22 8:19am    
Yeah, I see no reason to store that in the database.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900