Hide check box from controller-MVC - asp.net-mvc-2

How can i hide/show a check box based on a value obtained from controller.
I am using the given below code for check box
<%= Html.CheckBoxFor(m =>m.IncludeCallWithNoAgents) %>

Your view model could contain a property which would be set by the controller action and which should indicate whether this checkbox should be shown or not:
<% if (Model.ShouldShowCheckBox) { %>
<%= Html.CheckBoxFor(m => m.IncludeCallWithNoAgents) %>
<% } %>

Related

Silverstripe Blog Tags add class for current tag filter

Sorry I'm kinda new to Silverstripe.
Is there a way to add a active/current class to the BlogTagsWidget.ss list items?
Cheers
Assuming that the BlogController is the current controller when BlogTagsWidget.ss is rendered, you may be able to use this method directly to check it in the template, e.g.
<% loop $Tags %>
<% if $CurrentTag && $CurrentTag.ID == $ID %>
Current
<% else %>
Not current
<% end_if %>
<% end_loop %>
If it's not available then you may need to add some extra code to provide the current tag boolean in BlogTagsWidget::getTags().

How to access my model from my view?

I cannot seem to figure out how to access my Model from my View. I am confused.
Here is my Home controller. I have verified that "specimens" is being populated with data from the database:
public class HomeController : Controller
{
wildtropEntities wildlifeDB = new wildtropEntities();
public ActionResult Index()
{
ViewData["CurrentDate"] = System.DateTime.Now.ToString("MM/dd/yyyy");
var specimens = from s in wildlifeDB.specimen1
select s;
return View(specimens);
}
}
And here are couple snippets from my View:
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<% foreach (WildlifeTropical.Models.specimen s in ??????)
{ %>
<div>s.Name</div>
<% } %>
I assumed I would be able to access "specimens" since I passed it to the View from the Controller (ie, return View(specimens))...but it isn't working.
You will have to make your view strongly typed to a collection of specimen which is what you are passing to it from your controller action (IEnumerable<specimen>):
<%# Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<WildlifeTropical.Models.specimen>>" %>
<% foreach (WildlifeTropical.Models.specimen s Model) { %>
<div><%= Html.Encode(s.Name) %></div>
<% } %>
Notice how the view inherits System.Web.Mvc.ViewPage<IEnumerable<WildlifeTropical.Models.specimen>> and it is now strongly typed to a collection of specimens that you will be able to loop through.
This being said, personally I don't like writing foreach loops in my views. They make them look ugly. In this case I would use a display template:
<%# Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<WildlifeTropical.Models.specimen>>" %>
<%= Html.DisplayForModel() %>
and then I would define a display template which will automatically be rendered for each element of the model collection (~/Views/Shared/DisplayTemplates/specimen.ascx):
<%# Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<WildlifeTropical.Models.specimen>" %>
<div>
<%= Html.DisplayFor(x => x.Name) %>
</div>
See how the specimen.ascx user control is now strongly typed to System.Web.Mvc.ViewUserControl<WildlifeTropical.Models.specimen>. This is because it will be rendered for each specimen of the main view model.
I am not sure about MVC2. In MVC 3(with Razor View Engine), I can pass the Model / ViewModel to the View like this.
#model MyProject.ViewModel.UserViewModel
#{
ViewBag.Title = "Welcome To My Site";
}
<div class="divSubHead">
<h2>Hello #Model.FirstName</h2></div>
How about this? Note the Inherits for the Page.
<%# Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable(Of Specimen))" %>
<% foreach (WildlifeTropical.Models.specimen s in Model)
{ %>
<div>s.Name</div>
<% } %>

Ruby on Rails 3 Select Helper Question

Have have this line of code in my form when I create a new item. Though when I edit the item, the default selection isn't the one that is selected. Do I need to set the initial value?
<%= f.select :category, options_for_select(Item::CATEGORIES) %>
options_for_select accepts second param which identifies the selected value.
try
<%= f.collection_select :category_id, Item::CATEGORIES, :downcase, :titleize %>
It assumes your Item::CATEGORIES gives a array of strings of categories.
for each category in Item::CATEGORIES, category.downcase will be used as the option's value, while category.titleize will be used as the option's text.
ie.
<option value="<%= cate.downcase %>"><%= cate.titleize %></option>
======
or you could:
<%= f.select :category, options_for_select(Item::CATEGORIES, #cur_obj.category.id) %>

ASP.NET MVC2 TeplatedHelper doesn't render an ID of the HTML's markup

I have the code (snippet):
The Model is the IEnumerable object of the Person's class:
<% foreach (var item in Model)
{ %>
<tr>
<td><%= Html.DisplayFor(x=>item.Name) %></td>
</tr>
<% } %>
it renders only labels like that:
<td>Tommy</td>
According to the link it should be rendering a HTML markup something like:
but there is no the ID and the NAME property. Why ?
Your using the wrong template your should be using Html.EditorFor(x => x.Name)
Edit: I said you were using the wrong template because in your image it is a textbox displayed, not a label...
the default ouptut of Displayfor is
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.Encode(ViewData.TemplateInfo.FormattedModelValue) %>
according to Brad Wilson. You could easily build your own, look the other post of Brad Wilson for examples.
Or you could simply call Html.LabelFor(x => x.Name)
If you always want that, add a template, name String.ascx in your Views/Share/DisplayTemplate and just put the following in :
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.Label("", ViewData.TemplateInfo.FormattedModelValue) %>

Simple Asp.Net MVC2 Binding question: How to get an EditorFor to render a Radio Button Group?

Just thinking out loud about the subject, If I have the following:
<%= Html.EditorFor(m => m.Gender, "Gender")
And I want it to render two radio buttons:
O Male O Female
Assuming the class just has a string to hold the value, what would the Gender.ascx look like if we were to pass the Gender values using something like
ViewData["Gender"] = new string[] {"Male", "Female"};
This is a Radio button example (Yes No):
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.RadioButton("", "Yes") %> Yes
<%= Html.RadioButton("", "No") %> No
And another Radio Button set (Gender):
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.RadioButton("", "Male") %> Male
<%= Html.RadioButton("", "Female") %> Female
And a Drop Down Menu (Marital Status):
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.DropDownList("", new SelectList(new[] {"N/A",
"Single", "Married", "Divorced", "Widowed" })) %>
Now I need to modify these to look for a matching ViewData[""] list of options or figure out how to pass a selection list to the partial UI Template.