asp.net mvc 3 - Razor behaving strangely? -
razor playing tricks me. have partial view:
@model managemvc.models.default.classes.mvcmodule @{ if (model.canexpand) { response.write("crazy"); @:test <text>ojiiojjiojiojiojiojiojiojio</text> response.write("crazy2"); } else { response.write("crazy"); @:test <text>ssssssssdffffffff</text> response.write("crazy2"); } }
this called this:
@if (model.modules.count > 0) { (var = 0; < model.modules.count; i++) { html.partial("~/views/usercontrols/_menuitem.cshtml", model.modules[i]); } .... }
i expected razor print out whats written inside text block , @: block. nothing. when run prints crazycrazy2 (for each module in list). did miss something?
updated
the code calling if (model.modules.count > 0) partial itself. 1 called layout page. top code second partial being called. can make difference?
layout -> mainmenu (partial) -> createmenuitem (partial)
updated
this new code: (_menuitem.cshtml inside shared->displaytemplates)
@model managemvc.models.default.classes.mvcmodule @{ if (model.canexpand) { @:test <text>ojiiojjiojiojiojiojiojiojio</text> } else { @:test <text>ssssssssdffffffff</text> } }
partial view mainmenu calling menu item:
@html.displayfor(x => x.modules, "_menuitem")
now breaks on line , following error:
the model item passed dictionary of type 'system.collections.generic.list`1[managemvc.models.default.classes.mvcmodule]', dictionary requires model item of type 'managemvc.models.default.classes.mvcmodule'.
put @
when calling html.partial
helper , avoid using response.write
in asp.net mvc view:
for (var = 0; < model.modules.count; i++) { @html.partial("~/views/usercontrols/_menuitem.cshtml", model.modules[i]); }
also instead of writing ugly loops suggest using templated helpers. replace loop in layout simple helper:
@html.displayfor(x => x.modules)
and define display template (~/views/shared/displaytemplates/mvcmodule.cshtml
) rendered each element of modules
collection:
@model managemvc.models.default.classes.mvcmodule @if (model.canexpand) { @:test <text>ojiiojjiojiojiojiojiojiojio</text> } else { @:test <text>ssssssssdffffffff</text> }
see how easier is?
Comments
Post a Comment