Creating a div element on the server side using C# - asp.net-mvc-2

I am trying to create divs dynamically in a view page of an asp.net mvc project. This is the pseudo code:
<%
foreach (element in Model)
{
create the html div element with Div.id = Model.id
}
%>
I looked in the system.web.mvc.htmlhelper object. It provides support for a lot of html elements but not a div. Any Hints ?

There is no such helper for div. But, you can create your own HTML helper for it.
or simply you can go ahead and create divs in view page as
<%
foreach (element in Model)
{
%>
<div id="<%:element.id%>">
.. some html..
</div>
<%}
%>

Related

Dynamically add a form_tag using jQuery in Rails 3.2

My app has a user dashboard that lists rules a user has created. Each rule can be added to a "ruleset". For each rule, I want to display a button that when clicked, loads a form that allows the user to add that rule to a ruleset.
I have the following code in my view to show each rule:
<% #rules.each do |rule| %>
<div class="rule-wrapper">
<div class="rule-content row">
<div class="span6">
<strong>Rule:</strong> <%= rule.description %>
</div>
<div class="span2">
<div class="add-to-ruleset-form">
</div>
</div>
</div>
......
The pertinent part of my Rules controller is as follows:
class RulesController < ApplicationController
before_filter :authenticate_user!
def add_rule_to_ruleset
#ruleset = Ruleset.find(params[:ruleset_id])
#rule = Rule.find(params[:rule_id])
#ruleset.rule << #rule
end
....
Lastly, here is what my dashboard.js.erb file looks like:
$('.add-to-ruleset').click(function(e){
e.preventDefault();
$('.add-to-ruleset-form').html('<%= escape_javascript(link_to render :partial => "add_to_ruleset_form", :locals => {:rule => rule}) %>')
});
I'd like that when a button ('add-to-ruleset') is clicked, that the partial is rendered. I've tried using "escape_javascript" helper in my js.erb file but it isn't passing the "rule" variable corrent. I've also tried to use jQuery's load(), but I don't know how to pass the "rule" instance variable to the partial using jQuery. Is there another way?

Silverstripe loop over ALL children of the sitetree

I´m trying to create a sitemap of all children of the sitetree. For some pages ShowInMenus is set to false. But still these pages should be shown in the sitemao. I know you can loop over all children of a page like this (ignoring ShowInMenus):
<% loop AllChildren %>
$Title
<% end_loop %>
Can I do something similar on the top Level of the sitetree?
Thx,
Florian
i do not know any build in loop you can use for this but you can do in php
public function AllPagesWithParentIDZero() {
return SiteTree::get()->filter('ParentID', 0);
}
and use in template
<% loop $AllPagesWithParentIDZero %>
$Title
<% end_loop %>

Silverstripe: LinkingMode with Custom Controller

I have following problem:
Some links that show up in the Menu (the children of "Portfolio") are links to custom controllers. Of course now the LinkingMode is not available for that Links. Thats a image of the Menu:
So the children of Portfolio (Website, Application, etc.) are actually Category-DataObjects, which do not have a SiteTree Representation. The Submenu of Portfolio is created via checking and looping for all found categories in the Database.
The menu creation looks like that:
<ul>
<% loop Menu(1) %>
<li class="$LinkingMode">
[$LinkingMode] $MenuTitle.XML
<% if Children %>
<ul class="secondary">
<% if ClassName == 'ProjectsPage' %>
<% loop $Top.Categories %> <!-- loop all found categories, every found item links to the custom category controller -->
<li class="$LinkingMode">$Name</li>
<% end_loop %>
<% else %>
<% loop Children %>
<li class="$LinkingMode"><span class="text">$MenuTitle.XML</span></li>
<% end_loop %>
<% end_if %>
</ul>
<% end_if %>
</li>
<% end_loop %>
</ul>
Every Category (Website, Mobile) in the Menu links to a custom controller, which looks basically like that:
class Category_Controller extends Page_Controller {
public function show($arguments) {
return $this; //there will be more code to display all projects of a category
}
}
I expect that I have to add some custom code for the Category_Controller which tells the Portfolio Page which linkingmode it has...
Many thx,
Florian
I´ve found good tips here:
http://www.ssbits.com/snippets/2009/extending-linkingmode-to-handle-controller-actions/
http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-1-keeping-it-simple/
Thats the Category_Controller.php (a public var CategoryID is set there):
class Category_Controller extends Page_Controller {
public $CategoryID;
public function index($arguments) {
$slug = $arguments->param("Slug");
$category = Category::get()->filter(array('Slug' => $slug))->First();
$this->CategoryID = $category->ID;
}
}
DataObject Category (LinkingMode function checks if the current CategoryID set in the Controller equals the ID of the Category DateObject):
class Category extends DataObject {
public function LinkingMode(){
$categoryID = Controller::curr()->CategoryID;
return ($categoryID == $this->ID) ? 'current' : 'link';
}
}
In the template you can check then the linking mode as usual:
<% loop $Categories %>
<li class="$LinkingMode">$Name</li>
<% end_loop %>
Cheers,
Florian

asp.net MVC the microsoft edit template not including displaying object data

I used the edit template view for visual studio and it creates a nice form for me. The problem is that none of the objects data is included in the form. for example see this code this section:
<div class="editor-label">
<%: Html.LabelFor(model => model.VideoDesc) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.VideoDesc) %>
<%: Html.ValidationMessageFor(model => model.VideoDesc) %>
</div>
On the server side I have the following:
public ActionResult editvid(int id)
{
using (VideoDBEntities ent = new VideoDBEntities())
{
var vids = from myRow in ent.Videos
where (myRow.VideoId == id)
select myRow;
ViewData["model"] = vids.ToList()[0];
}
return View();
}
I am new to MVC and LINQ and trying to find my feet
thanks
Andy
If you are using a strongly typed view you can pass your video object as model.
It is done by sending it as a parameter in View() method.
View can be overloaded with model object, if you have no model you can leave it empty.
in this case you can simply define video variable and pass it to the View.
using (VideoDBEntities ent = new VideoDBEntities())
{
var video = ent.Videos.SingleOrDefault(x=> x.VideoId ==id);
return View(video );
}

Hieararchical data in MVC

In webforms I used a repeater inside a repeater(Hieararchical model). How can I achieve the same in MVC?
Example:
Data1 Data2
subdata1 subddata3
subdata2 subdata4
Data3 data4
subdata5 subdata7
subdata6 subdata8
I also require a two column layout as shown above. Any ideas ??
I can't remember where I read this, but it applies to you:
-- But won't we nead at least a repeater control in MVC?
-- We have a repeater control. It's called a for each loop
Let's say your view model has a property called Data of type IEnumerable<SuperDuper>. To iterate over it, you'd just do
<% foreach (var sd in Model.Data) { %>
<!-- write out fancy stuff -->
<% } <%>
To iterate over subdata, let's say that SuperDuper has a property named SubData that's also an IEnumerable<Something>. Nothing stops you from doing
<% foreach (var sd in Model.Data) { %>
<!-- write out some fancy stuff -->
<% foreach (var sub in sd.SubData) { %>
<!-- write out some more fancy stuff -->
<% }
} %>
For the two-column layout, resort to CSS.
And since Razor is on it's way, I can't resist to show you what those examples would look like with the new engine:
#foreach (var sd in Model.Data) {
<!-- write out fancy stuff -->
}
#foreach (var sd in Model.Data) {
<!-- write out some fancy stuff -->
#foreach (var sub in sd.SubData) {
<!-- write out some more fancy stuff -->
}
}
And with spark view engine you do this:
<div each="var item in Model.Data">
${item.Title}
<div each="var subItem in item.SubData" style="padding-left: 20px">
${subItem.Title}
<!-- Do some fancy stuff -->
</div>
</div>
Razor? Yuck! ;)