asp.net mvc - How to use Linq objects to validate a view in MVC 2 -
i use linq , typed views in right way. @ moment following:
make model verify agianst:
public class menu { public int id { get; private set; } public string text { get; private set; } public string action { get; private set; } public string controller { get; private set; } public string parameter { get; private set; } public string langue { get; private set; } public menu(int id, string controller, string action, string parameter, string text) { id = id; controller = controller; action = action; text = text; parameter = parameter; }
use linq data database model:
public static list<menu> gettablistformenu(string langue) { page_dbentities entity = new page_dbentities(); var tablist = (from ml in entity.wpmenulangue ml.langue == langue m in entity.wpmenu ml.menu == m.id l in entity.wplangue ml.langue == l.langue p in entity.wppage p.id == m.page select new { m.id, p.controller, p.action, p.parameter, ml.text}).tolist(); list<menu> menu = new list<menu>(); foreach (var item in tablist) { menu.add(new menu(item.id, item.controller, item.action, item.parameter, item.text)); } return menu; }
i pretty convinced not optimal way , have 2 questions:
when data database first use var , have move object foreach. seems waste of both time , less effeicent getting sql.
i have been told can verify agianst entitymodel. if use multiple entities in view. true? (the 1 telling me wes not able work , have not been able find online).
i try on post in next couple of hours, might have wait 24 hours.
public static list<menu> gettablistformenu(string langue) { page_dbentities entity = new page_dbentities(); return (from ml in entity.wpmenulangue ml.langue == langue m in entity.wpmenu ml.menu == m.id l in entity.wplangue ml.langue == l.langue p in entity.wppage p.id == m.page select new menu(m.id, p.controller, p.action, p.parameter, ml.text) ).tolist(); }
as validation concerned shouldn't use multiple entities in view. should use single entity called viewmodel. viewmodel class represents data on view. if using dataannotations validation decorate view model properties attributes indicate how validated.
Comments
Post a Comment