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


i used database first approach create register table student_id primary key,auto-incremented , asked dob,email ,name , contact info. i'm connected connect accounts table foreign key student_id common in both tables.in accounts table accounts_id primary key.now when use crud operations in column of studentid displays student name instead of studentid.now want display list of studentid register table accounts table. accounts.controller code

   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 , entity framework. account create: enter image description here database: enter image description here

thanks :)

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

first, please note account shouldn't have student name, student name in register table, , use student id foreign key used map original register student name from, can't have redundant fields, not @ all.

now short answer question:

it displaying name of course

why ?

new selectlist(db.registers, "student_id", "student_name", account.student_id); 

which

public selectlist(     ienumerable items,     // list of data     string datavaluefield, // value, id     string datatextfield   // text, name ); 

now can change student_name student_id both text , value id, display both,

// students var thesutdents = db.registers.asnotracking().tolist(); foreach(var student in thestudents) {  // make name equal name - id student.student_name = student.student_name + " - " + student.student_id; } //then select list, name both name , id, value still id new selectlist(thesutdents , "student_id", "student_name", account.student_id); 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -