I have a simple mvc application in which I have login form in this login form I have student email and password which are coming from the text boxes this view is strongly type and binding with login model ,now i wanted to add one more field to be passed to controller from view and this new field is in anothet table like student house number ,i have added the property as public studentadrrss addrss,as here studentadrrss is a entity it comes in controller but emplty i want to get the student adress of that specific student and pass in controller or to get it in controller and pass in view how to do that
Loading

Chetan SanghaniRank #244 · 10.2K reputationPosted Jul 13, 2024, 2:13 AM EDT
To achieve this, you need to pass both the login information and the student address from the view to the controller. Here’s how you can implement this in your ASP.NET MVC application:
Step-by-Step Implementation
Update the Login ViewModel:
First, add the
StudentAddressproperty to yourLoginViewModel.public class LoginViewModel { public string Email { get; set; } public string Password { get; set; } public StudentAddress Address { get; set; } }Assuming
StudentAddressis another model representing the student address details.public class StudentAddress { public string HouseNumber { get; set; } // Add other address fields as necessary }Update the Login View:
Update your view to include the new address fields. Ensure the view is strongly typed to
LoginViewModel.@model YourNamespace.Models.LoginViewModel @using (Html.BeginForm("Login", "Account", FormMethod.Post)) { @Html.LabelFor(m => m.Email) @Html.TextBoxFor(m => m.Email) @Html.LabelFor(m => m.Password) @Html.PasswordFor(m => m.Password) @Html.LabelFor(m => m.Address.HouseNumber) @Html.TextBoxFor(m => m.Address.HouseNumber) Login }Update the Controller:
In your controller, receive the
LoginViewModeland process the information.public class AccountController : Controller { [HttpGet] public ActionResult Login() { return View(); } [HttpPost] public ActionResult Login(LoginViewModel model) { if (ModelState.IsValid) { // Fetch student address from the database if it's not passed from the view if (model.Address == null) { var address = GetStudentAddress(model.Email); model.Address = address; } // Authenticate the user var student = AuthenticateStudent(model.Email, model.Password); if (student != null) { // Successful login return RedirectToAction("Index", "Home"); } else { // Invalid login ModelState.AddModelError("", "Invalid login attempt."); } } return View(model); } private StudentAddress GetStudentAddress(string email) { // Fetch student address based on the email from the database // Example: using (var context = new YourDbContext()) { var student = context.Students.Include(s => s.Address) .FirstOrDefault(s => s.Email == email); return student?.Address; } } private Student AuthenticateStudent(string email, string password) { // Authenticate student using email and password using (var context = new YourDbContext()) { var student = context.Students .FirstOrDefault(s => s.Email == email && s.Password == password); return student; } } }Explanation
LoginViewModel:
StudentAddressproperty to hold the student's address information.Login View:
LoginViewModel.AccountController:
Loginaction to receive theLoginViewModel.GetStudentAddressto fetch the student's address based on the email if it's not passed from the view.Loginmethod to authenticate the student and handle the address information.By following these steps, you will be able to pass the student address information from the view to the controller or fetch it in the controller and pass it to the view as needed.