How to get list of student id which is primary key in mvc dot net application for web application

Home / Uncategorized / How to get list of student id which is primary key in mvc dot net application for web application

Question:
I used the database first approach to create Register table with Student_id as primary key,auto-incremented and also asked for DOB,Email ,Name and Contact info. I’m connected it to connect it to an accounts table with foreign key Student_Id which is common in both the tables.In my accounts table Accounts_Id is primary key.Now when i use CRUD operations in the column of StudentID it displays the Student Name instead of the Studentid.Now i want to display list of StudentId from the register table into the Accounts table. My accounts.controller code is
public ActionResult Create([Bind(Include = "Account_Id,Student_Id,Student_Name,Course_Id,Course_Name,Certification_Number")] Account account)
{
if (ModelState.IsValid)
{
db.Accounts.Add(account);
db.SaveChanges();
return RedirectToAction("Index");
}

ViewBag.Course_Id = new SelectList(db.Courses, "Course_Id", "Course_Name", account.Course_Id);
ViewBag.Student_Id = new SelectList(db.Registers, "Student_ID", "Student_Name", account.Student_Id);
return View(account);
}

I’m using mvc5 and entity framework. Account create: Database:

Thanks for the help 🙂

Edit
public partial class Account
{
public int Account_Id { get; set; }
public int Student_Id { get; set; }
public string Student_Name { get; set; }
public int Course_Id { get; set; }
public string Course_Name { get; set; }
public Nullable Certification_Number { get; set; }

public virtual Cours Cours { get; set; }
public virtual Register Register { get; set; }
}

}

Create file in View
@Html.AntiForgeryToken()

Account


@Html.ValidationSummary(true, "", new { @class = "text-danger" })

@Html.LabelFor(model => model.Student_Id, "Student_Id", htmlAttributes: new { @class = "control-label col-md-2" })

@Html.DropDownList("Student_Id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Student_Id, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Student_Name, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.Student_Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Student_Name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Course_Id, "Course_Id", htmlAttributes: new { @class = "control-label col-md-2" })

@Html.DropDownList("Course_Id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Course_Id, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Course_Name, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.Course_Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Course_Name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Certification_Number, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.Certification_Number, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Certification_Number, "", new { @class = "text-danger" })

}


Answer:
First, Please Note that your account shouldn’t have student name, student name will be in register table, and you will use student id as a foreign key which will be used to map to the original register where you will get student name from, you can’t have redundant fields, it is not good at all.

Now short answer for your question:

it is displaying name of course

why ?
new SelectList(db.Registers, "Student_ID", "Student_Name", account.Student_Id);

which is originally
public SelectList(
IEnumerable items, // List Of data
string dataValueField, // this is value, the id
string dataTextField // this is text, the name
);

now you can change student_name to student_id then both text and value will be the id, but what i usually do is display both, something like this
// Get All Students
var TheSutdents = db.Registers.AsNoTracking().ToList();
foreach(var student in TheStudents)
{
// Make name equal to name – ID
student.Student_Name = student.Student_Name + " – " + student.Student_ID;
}
//then my select list, Now the name is both name and ID, the value is still the id
new SelectList(TheSutdents , "Student_ID", "Student_Name", account.Student_Id);
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *