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<int> Certification_Number { get; set; }
public virtual Cours Cours { get; set; }
public virtual Register Register { get; set; }
}
}
Create file in View
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Account</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Student_Id, "Student_Id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Student_Id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Student_Id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Student_Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Student_Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Student_Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Course_Id, "Course_Id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Course_Id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Course_Id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Course_Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Course_Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Course_Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Certification_Number, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Certification_Number, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Certification_Number, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}