c# - asp.net Complex models Single View -
i have above database model:
- pd: predefined (user has list of options choose from)
- ua: user added (user can create new entry's this)
- sa: system added (generated in controller when saved)
i'm trying think of way of creating 'over view' can create requirements report_main on 1 page instead of having create view report_main directing user page them enter report_peramiter_fields etc.
i've had @ view models cant quite head around it, 'index' , 'detail' views pulling data back. more creating , editing models.
database: sql server data mapping: entity framework view engine: razor
viewmodels way represent data on view need display information models, please have @ question understand are: what viewmodel in mvc?
in case can create simple viewmodel represent data need display on report_main, can achieved doing:
namespace myproject { public class myviewmodel { public int id { get; set; } public system_tracking tracking { get; set; } public report_login_credentials credentials { get; set; } public report_type type { get; set; } public list<report_peramiter_fields> fields { get; set; } public string name { get; set; } public string location { get; set; } } } it may simple model, because it's used view , not persisted. difference here between model , view model.
from need 1 page, mypage.cshtml can use model like:
@model myproject.myviewmodel @{ viewbag.title = "mypage"; } @* content here *@ @model.id <br> @model.report_type.description // etc. to pass information need in controller, example:
namespace myproject { public class mycontroller : controller { public actionresult mypage(int? id) { var data = context.report_main.find(id); // or whatever var vm = new myviewmodel(){ tracking = data.system_tracking, // ... populate viewmodel here data received }; return view(vm); } } } in short, viewmodels allow bind data in 1 model , pass onto view can statically represent it, , can use when getting data user when don't want direct representation or access model.
edit: can iterate through list of models in razor using c#:
foreach(var item in model.fields) { <p>item.peramiter</p> } 
Comments
Post a Comment