-1

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: enter image description here Database: enter image description here

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>
}

  • you are showing `post` . show your `get` code –  Aug 15 '17 at 06:26
  • Currently you're showing `HttpPost` method. Where is `HttpGet` one? I think using `new SelectList(db.Registers, "Student_ID", "Student_ID", account.Student_Id)` just enough. – Tetsuya Yamamoto Aug 15 '17 at 06:27
  • 1
    All you want is the dropdownlists for `Student_Id` and `Course_Id` Delete you database fields (and textboxes in the view) for the `Student_Name` and `Course_Name`. You already have a relationship between the tables and you should not be duplicating those 2 fields –  Aug 15 '17 at 06:40

2 Answers2

0

I wanted to write this as a comment because I am not 100% sure that's the problem, but couldn't because I lack of reputation.

In your SelectLists, change the third parameters to "Course_Id" and "Student_Id".

ViewBag.Course_Id = new SelectList(db.Courses, "Course_Id", "Course_Id", account.Course_Id);

ViewBag.Student_Id = new SelectList(db.Registers, "Student_ID", "Student_ID", account.Student_Id);

The second parameter in SelectList is the value behind the SelectListItem, while the third one is the text that is shown to the user.

Check this thread and this thread for better explanation if you didn't get me.

0

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);
Munzer
  • 2,216
  • 2
  • 18
  • 25