Sunday, 15 January 2012

asp.net mvc 4 - MVC4 Entity Framework 5 Many-To-Many Save Entity to Database -



asp.net mvc 4 - MVC4 Entity Framework 5 Many-To-Many Save Entity to Database -

hi i've been stuck long on problem. i've looked @ lot of examples cant find im looking for. help appreciated.

i utilize code-first approach.

i've enabled migration , database fine [student] - [studentcourse] - [course].

scenario: have 2 entites -> student

public class pupil { public int id { get; set; } public string name { get; set; } public virtual list<course> courses { get; set; } }

and course

public class course of study { public int id { get; set; } public string name { get; set; } public string description { get; set; } public virtual list<student> students { get; set; } }

nothing fancy that... ive created viewmodel ->

public class studentcourseviewmodel { public int id { get; set; } public string name { get; set; } public list<course> courses { get; set; } }

view:

@model project.web.models.studentcourseviewmodel @{ viewbag.title = "edit"; }

edit

@using (html.beginform()) { @html.validationsummary(true)

<fieldset> <legend>student</legend> @html.hiddenfor(model => model.id) <div class="editor-label"> @html.labelfor(model => model.name) </div> <div class="editor-field"> @html.editorfor(model => model.name) @html.validationmessagefor(model => model.name) </div> <div class="editor-label"> @html.labelfor(model => model.courses) </div> <div class="editor-field"> @for (int = 0; < model.students.count(); i++) { <div style="border: dotted 1px; padding: 5px; margin: 10px;"> @html.hiddenfor(s => s.students[i].id) @html.labelfor(s => s.students[i].name[i + 1]) @html.editorfor(s => s.students[i].name) </div> } </div> <p> <input type="submit" value="save" /> </p> </fieldset> }

controller action:

[httppost] public actionresult edit(coursestudentviewmodel model) { var course of study = db.courses.find(model.courseid); course.name = model.coursename; course.description = model.coursedescription; course.students = model.students; if (modelstate.isvalid) { db.entry(course).state = system.data.entitystate.modified; db.savechanges(); homecoming redirecttoaction("index"); } homecoming view(model); }

(maby go wrong...)

anyway, i want create new pupil optional many courses (textboxes -> coursename)

how should this?

the main issue null values (student fine, list of courses = null) view [httppost]create -action.

i'm in need of guidance how create approach possible.

thx j!

your entities not setup correctly many-to-many relationship. need entity handle many-to-many mapping. this.

public class studentstocourses { public int studentid {get; set;} public int courseid {get; set;} public virtual pupil student {get; set;} public virtual course of study course {get; set;} }

then pupil model should changed this.

public class pupil { public int id { get; set; } public string name { get; set; } public virtual list<studentstocourses> courses { get; set; } }

and coursed model changed this.

public class course of study { public int id { get; set; } public string name { get; set; } public string description { get; set; } public virtual list<studentstocourses> students { get; set; } }

you need setup foreign key relationship using fluent api. this.

public class studentstocoursesconfiguration : entitytypeconfiguration<studentstocourses> { internal studentstocoursesconfiguration () { this.haskey(p => new {p.studentid, p.courseid}); this.hasrequired(p => p.student) .withmany(p => p.courses) .hasforeignkey(p => p.studentid); this.hasrequired(p => p.course) .withmany(r => r.students) .hasforeignkey(p => p.courseid); } }

entity-framework asp.net-mvc-4

No comments:

Post a Comment