How can I list date values as enumerable values list for ListBox - visual-studio-2017

I'm loading a ListBox from a model which looks like this
public class FilterModel
{
public Learning TypeOfLearning { get; set; }
public TeachingYear Teaching { get; set; }
}
public enum Learning
{
All,
Frameworks,
Qualifications,
Units,
Standards
}
public enum TeachingYear
{
2016/2017,
2017/2018,
2018/2019,
2019/2020
}
The values are loaded into my ListBox as below
#Html.DropDownList("LearningType", new SelectList(Enum.GetValues(typeof(Learning))),
"Select Learning Type", new { #style = "width: 350px" })
#Html.DropDownList("TeachingYeare",
new SelectList(Enum.GetValues(typeof(TeachingYear))),
"Select Teaching Year", new { #style = "width: 350px" })
Learning works fine but TeachingYear objects to the numeric values which I've tried parsing but it still wont run. How do I set up my enum to correctly display the date range values?

As per MSDN documentation,
An enumeration is a set of named constants whose underlying type is any integral type
Being constants, their name can't be defined using numbers as their first character, just like any other variable. Also, considering that every teaching year will always finish the year after it started, putting the ending year in the name itself is redundant. I suggest you should change the name and use attribute decorators like this:
using System.ComponentModel.DataAnnotations;
public enum TeachingYear
{
[Display(Name = "2016/2017")]
_2016,
[Display(Name = "2017/2018")]
_2017,
[Display(Name = "2018/2019")]
_2018,
[Display(Name = "2019/2020")]
_2019
}
so that, when using the EnumDropDownListFor html helper, descriptions are automatically shown in the list, while values are bound to the object. See also this answer here.
EDIT:
Considering your latest comment about the framework, in the view
#Html.GetEnumSelectList<TeachingYear>()
should do it, but I’ve never used .NET Core so you’d better check the relative MSDN documentation for proper usage.

In your View , you could try the following changes :
<div class="form-group">
<select asp-for="learning"
asp-items="#Html.GetEnumSelectList<Learning>()">
<option>Select Learning Type</option>
</select>
</div>
<div class="form-group">
<select asp-for="learning"
asp-items="#Html.GetEnumSelectList<TeachingYear>()">
<option>Select Teaching Year</option>
</select>
</div>

Related

Sitecore Building a single page from child node data

I have a Sitecore structure of items which comprises of
Range
Product 1
Product Name (text)
Product Image (image)
Product 2
Product Name (text)
Product Image (text)
I need to make a single page view that iterates through each of these nodes and collects and outputs the data for each - can anyone assist in the method I would best use to do this?
Sorry if this is a basic question but any example code would be appreciated.
You should really think about just using a single product template with a Product Name field and a Product Image field instead of having items with single fields under the product. But if this is your requirement, this is how you would do it.
SubLayout (or layout if need be)
<div>
<sc:Text runat="server" id="txtProductName" Field="ProductName"/>
<sc:Image runat="server" id="imgProductImage" Field="ProductImage"/>
</div>
Then in code behind you would take the current item (the product and find the child item that corresponds to what you are looking for and assign it as the field item.
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
txtProductName.Item = Sitecore.Context.Item.Children.FirstOrDefault(x => x.TemplateID == Constants.Templates.ProductName);
imgProductImage.Item = Sitecore.Context.Item.Children.FirstOrDefault(x => x.TemplateID == Constants.Templates.ProductImage);
}
In my example I am resolving it looking for an item of template type X, but you could go by name or some other way of knowing.

Ember saving one to one, using promises

im trying to save a few models with one to one relation which initializes on a single controller action. Ive split things into small functions to use promises. I've been trying to make it work for almost a week and cant find the solution. I've also made a jsbin:
http://jsbin.com/enoYONAv/1/edit
Instead of valueBinding you can use selectionBinding in your Ember.Select, to retrieve the selected model directlly instead of your id. For example:
{{
view Ember.Select
prompt="Gender"
contentBinding="genders"
optionValuePath="content.id"
optionLabelPath="content.type"
selectionBinding="selectedGender"
}}
So in you action you can do:
var gender = this.get('selectedGender');
...
var hash = {
name : personName,
gender : gender,
organization : organization
};
this.savePerson(hash).then(this.saveAgentAssociation(agentAddress));
This is your updated jsbin please give a look http://jsbin.com/efeReDer/1/edit

How to create a directory-like structure with meteor templates?

Consider this mockup of my intended output:
The data structure behind this in a Mongo DB looks like this - I did not nest the subcategories inside the document because I still want to be able to update subdocuments atomically - and I also want to allow a dynamic amount of layers below. From what I've seen, Mongo currently does not allow for easy, dynamic access of nested documents.
topmost_category = {
_id : "foo",
category_name : "Example",
parent_category: null,
subcatories: [ "sub_foo1", "sub_foo2", ... ]
}
child_category = {
_id = "sub_foo1",
category_name : "Example 2",
parent_category: "foo",
subcategories: []
}
The underlying HTML consists simply of nested -branches. The currently selected category gets an "active" class, and the icons in front are named "icon-folder-close" and "icon-folder-open" (<i class="icon-folder-close"></i>).
Now, I could use the answer to this to create a complete tree-like structure. However, all the branches would be "open". How do I make it so that only the currently selected branch is visible like shown in my mockup (and still have it reactive to boot)?
You can see the implementation of a very similar approach to that described by Hubert here:
http://www.meteorpedia.com/read/Trees
It's the code to the (working) category browser functionality of the wiki (you can see a demo there).
The main difference to Hubert's answer is that it uses a publish/subscribe with a session variable to fetch the data of opened children in real time.
The easiest way would be to add a boolean flag for branches denoting whether or not they belong to current path, and therefore should be open.
<template name="branch">
{{#if open}}
<div class="openBranch">
...
{{#each children}}
...
{{/each
</div>
{{else}}
<div class="closedBranch">
...
</div>
{{/if}}
</template>
Now, the setting and management of this flag depends on the logic of your application. Where do you store the currently selected branch?
I assume the state is limited to a single client. In this case, one solution is to:
maintain an array of opened branches ids, scoped to a single file,
implement the open flag as a helper:
Template.branch.open = function() {
return _.indexOf(openedBranches, this._id) !== -1;
}
when user changes selection, clear the array and populate it with path from the selected branch to the top.

Setting selected entry using helper.options?

Im am using Play 2.0.4 and
helper.options(myitems)
in my template (inside of a helper.select)
In this case, how can I define the default selected entry, which shall be one entry out of myitems? Thanks for any hint!
A little bit more about my case:
Imagine a news archive, showing all news titles. This news archive uses pagination, pagination uses GET to pass the next/previous page number.
The play framework however will only correctly select the currently selected "select" item (here: news category) when a POST request was used - while pagination uses GET!
Intended behaviour: While a filter is applied / a specific news category is selected, this shall always be visible to the user by preselecting the currently selected news category in the "select" form.
A "screenshot" for illustration:
So, anyone having a good idea on how to cope with this problem? Any way to tell Play manually which entry from the "select" form it shall select? '_default always adds a new entry instead of selecting one out of the given options ): Would be great, if one wouldn't have to build the complete "select" form manually.
Try passing '_default option to select helper:
#import views.html.helper._
#select(form("email"), options(List("first", "third")), '_default -> "second")
It seems, unfortunately, the only way to figure it out is to look up the source.
Update:
Specifying _default property doesn't set selected attribute on option tag. It looks like the only way to preselect entry is to pass prefilled form to the template. For example, suppose you have following form:
case class RegInfo(email: String, color: String)
private val registrationForm = Form(
mapping(
"email" → email,
"color" → nonEmptyText(minLength = 5, maxLength = 32)
)(RegInfo.apply)(RegInfo.unapply)
)
Then in the action prefill form before passing to the view:
def create = Action {
Ok(html.create(registrationForm.fill(RegInfo("user#qwe.com", "blue"))))
}
Then in template use helper:
#select(form("color"), options(List("red", "green", "blue")))
And value will be preselected.
Ended up with the pragmatic approach:
<select id="myfield" name="myfield" >
<option class="blank" value="">-- All items --</option>
#for((key, value) <- MyModel.options) {
#if(key == GETValuePassedToTemplate) {
<option value="#key" selected>#value</option>
} else {
<option value="#key">#value</option>
}
}
</select>
Still wondering if there is a better option / way to do it.
Actually, there is a nicer solution to it. If you call the template having the form partially bound you will achieve your goal. Here's the code for your controller:
Ok(views.html.myForm(myForm.bind(
Map("fieldName1" -> "value1",
"fieldName2" -> "value2"))))
Make sure you map fieldnames to the values of the options you want pre-selected.
Still wondering if there is a better option / way to do it.
Well, if your not hell-bent on using play to solve this particular problem, you could always solve it using JavaScript and jQuery:
$(function () {
$('#yourSelect_id').val(5);
}
Where your select options each has values and the one option you whish to pre select has value 5.

WP7 Pivot Title Template DataContext Binding not working

I have a Pivot Title Template so that the Pivot page has a title and subtitle. I want to set both via code.
I build the XAML in Blend and without code binding, it does display so that part works.
However, my binding isn't working. It either won't build because the object doesn't have a DataContext, or the object doens't exist in current context or does build but won't display. When it doesn't display, I assume I'm binding to the wrong XAML object.Each object is named only so I could find the right object to bind to.
The containing class for the bound code of Title and Subtitle looks like:
public class PivotTitle
{
public string Title = "";
public string Subtitle = "";
}
My question is: how do I correctly bind the TitleTemplate so that the two TextBlocks' Text properties can be set in code?
Here is the XAML
<controls:Pivot.TitleTemplate >
<DataTemplate x:Name="PivotTitleTemplateDataTemplate" >
<StackPanel x:Name="MyPivotTitle" DataContext="{Binding}" >
<TextBlock x:Name="Title"
Text="{Binding Title}"
FontSize="20"
TextWrapping="Wrap"/>
<TextBlock x:Name="Subtitle"
Text="{Binding Subtitle}"
Foreground="Gray"
TextWrapping="Wrap"/>
</StackPanel>
</DataTemplate>
</controls:Pivot.TitleTemplate>
The title and subtitle are dependent on the page navigated from. The code behind looks like:
//defined at top of page class
public PivotTitle _PivotTitle = new PivotTitle();
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string queryStringDeploymentName = "";
this.NavigationContext.QueryString.ContainsKey("DeploymentName"))
_PivotTitle.Title = SelectedDeployment.DeploymentName;
_PivotTitle.Subtitle = App.ViewModel.AppSettings.UpdatedText;
MyPivotTitle.DataContext = _PivotTitle;
}
This particular example won't build with this error: The name 'MyPivotTitle' does not exist in the current context. The binding on the Pivot page's Listbox is working correctly.
I believe my codebehind and overall XAML are correct. I think I'm DataContext binding incorrectly.
Thanks for any help.
In your example MyPivotTitle is an element within the DataTemplate, so cannot be accessed directly from your code, hence the error. If you need to access a control inside a template of an items control (such as Pivot or ListBox`) then you may find this article from WIndowsPhoneGeek.com useful.
However, it sounds like you might be able to do this without any "hacking". Is this title and subtitle just for a single pivot item, or will it be on all of them? What other data, if any are you binding on the pivot item(s)?