Saturday, 15 February 2014

c# - Custom queries in MVC 4 / Entity Framework? -



c# - Custom queries in MVC 4 / Entity Framework? -

i have working crud application built using asp.net mvc 4 , entity framework. utilize edmx model tables, deployed in database on live server.

i want know how query tables in controllers homecoming view contains columns each table (join), 1 table, based on column header returned query string

these tables, have getters , setters them in respective models , model "model1.edmx"

class="lang-none prettyprint-override">acccompany acccontrol id id code controlcode companyid nominal accountscompany costcentre company section

i don't understand how bring together tables using custom methods, mvc framework. ef seemed - in terms of actual query...

there 2 approaches like.

the first straight forward, using navigation methods entity framework:

controller:

public actionresult details(short id = 0) { acccompany acccomp = db.acccompany.find(id); if (acccomp == null) { homecoming httpnotfound(); } homecoming view(acccomp); }

view:

@model some.entities.acccompany <div class="displaylabel"> @html.displaynamefor(model => model.company) </div> <div class="displayfield"> @html.displayfor(model => model.company) </div> <div class="displaylabel"> @html.displaynamefor(model => model.acccontrol.costcentre) </div> <div class="displayfield"> @html.displayfor(model => model.acccontrol.costcentre) </div>

the sec 1 involves creating custom "view model" specific view , using model in view, improve validations imo:

someviewmodel.cs:

public class someviewmodel { [required] public string company { get; set; } [required] [display(name = "cost centre")] public string costcentre { get; set; } }

then populate in controller:

public actionresult details(short id = 0) { acccompany acccomp = db.acccompany.find(id); if (acccomp == null) { homecoming httpnotfound(); } someviewmodel vm = new someviewmodel(); vm.company = acccomp.comany; vm.costcentre = acccomp.acccontrol.costcentre; homecoming view(vm); }

then view:

@model some.someviewmodel <div class="displaylabel"> @html.displaynamefor(model => model.company) </div> <div class="displayfield"> @html.displayfor(model => model.company) </div> <div class="displaylabel"> @html.displaynamefor(model => model.costcentre) </div> <div class="displayfield"> @html.displayfor(model => model.costcentre) </div>

hope helps

c# asp.net sql asp.net-mvc-4

No comments:

Post a Comment