How to get all selectlist items in razor page? - razor-pages

I have a select list:
[BindProperty]
public List<SelectListItem> FooList { get; set; }
The list is then instantiated as an empty list and the razor page loads. The list is then populated by the user on the client side using jquery. The server doesn't know the items in the list until the form is posted. So far, so good.
In the page I'm using the select tag helpers like so...
<select asp-for="FooList" multiple="multiple" asp-items="FooList" class="form-control form">
I'm wondering how can I retrieve all items in a mulitple selectlist "list box" (whether they're selected or not) OnPost using model binding?
I'm using razor pages 2.2 and I'm really struggling to figure this out.
public IActionResult OnPost()
{
// not sure how to get the selectlist and all of its items?
foreach (var item in FooList)
{
// do stuff with the items
}
return Page();
}
I know it's got to be something simple, just need a fresh set of eyes to point me in the right direction. Thanks!

I'm wondering how can I retrieve all items in a mulitple selectlist
"list box" (whether they're selected or not) OnPost using model
binding?
You can't. Only the values of the selected items are posted and can take part in model binding. If you want to get the selected items in a multiple select list, you need to bind them to a collection. Your asp-for attribute should reference the name of the collection that you are binding the posted values to. You should not have [BindProprty] on the List<SelectListItem> type. They only represent the items that populate the select list.
You can find out more about the basics of Select Lists in Razor Pages here.

Related

How to save custom form control input data in web part?

I followed the "Developing Custom Form Control" in kentico documentation and built a custom list box. I added the list box dynamically on the code behind and NOT adding it directly on the code front (ascx). I use the list box on one of my web parts and everything works well when I selected multiple items. However, when I click to edit the web part, all of the selected items are gone and the the list box comes back to its original form ( no item selected ). Therefore, I wonder how kentico save the old data of the form control in the web part.
On the code below, I recreate my scenario with a short version. I dynamically add the list box under a panel.
protected void EnsureItems()
{
// Create item and list box
ListBox tab = new ListBox();
ListItem item = new ListItem();
item.Text = "test";
tab.Items.Add(item);
panel.Controls.Add(tab);
}
protected void Page_Load(object sender, EventArgs e)
{
EnsureItems();
}
Each Form Control should be inherited from FormEngineUserControl. And Kentico utilizes Value property then to store and retrieve values from the db. Here is the example:
public override object Value
{
get
{
return listBox.SelectedValue;
}
set
{
listBox.SelectedValue = ValidationHelper.GetString(value, string.Empty);
}
}
Basically, your getter should return some value to be stored in the database. And in the setter you should initialize your listbox, fill with data and make a selection base on value coming from the database.
Basically, a form control itself doesn't save data to the database. The form control is attached to some form and the form saves the data to the database. Check out the documentation regarding custom form controls.

How to populate datalist on new page in View Scope?

I have a dashboard with number of items on it. One of them is "Teachers". I am using <h:link> to go to show-teachers.xhtml which has a search box on it initially. Scope of this bean is view scope and it has two methods:
getAllTeachers - This method is void as it is called when user clicks on the search button and results are populated on the page using datalist and rendered is set to true to display the datalist. This work fine.
getAllStudentsByTeacher - Once I have the list of teachers from above method the next step is to fetch the list of students under a specific teacher and list them using datalist on a new page(show-students.xhtml).
Above method fetches the list of students from the database and return string show-students which is another XHTML page on which I want to show the list of matching students using datalist.
But I always get result No records found on show-students page.
When I change the scope of this bean to session, it works fine. But I would like to use either request or view scope.
Please suggest me If I am missing anything as I am learning JSF/Primefaces :)
Thanks
Rahul Saraswat

How to add found item to a QListView using Qt?

I am trying to implement search option for my file browser application.
I can get the item after taking an input from the user. Now, I want to add this item to my listview. Also after the search is over, the item should be clickable to open them.
Here, is the piece of code... Any suggestions will be appreciated.
void Browser::search()
{
QDirIterator it(path,QDir::AllDirs|QDir::Files|QDir::NoSymLinks|QDir::NoDotAndDotDot,QDirIterator::Subdirectories);
while(it.hasNext())
{
it.next();
if(it.fileInfo().completeBaseName().contains(content,Qt::CaseInsensitive))
{
qDebug()<<"it.fileinfo = "<<it.fileInfo().fileName();
}
}
path.clear();
}
Instead of List View you can use List Widget and simply fill the list using method addItem or addItems. If your list is small/simple it doesn't in my opinion make sense to use Model-View paradigm.
Look at QListWidget in documentation
You should read more about model/view concept. You add data to your model not to a view. You'll find in this article sections "Editable items" and "Resizable models", which address your issue.

Person & Group custom field type sharepoint

I have created a custom field type as it is there in sharepoint OOTB, the difference is only that the end user does not need to check the name i.e I have replaced it with DropDownList. The dropdownlist suggest the no. of users available in the web site for that I have created a FieldClass which inherits from SPFieldUser and a FieldControlClass which inherits from UserField. It is working fine in all conditions i.e when I create a List or Document Libarary it shows me the DropDownList
with respective users after saying OK it creates an item for me. I have overriden a Value property in FieldControlClass as follows,
public override object Value
{
get
{
SPUserCollection userscollection = rootWeb.SiteUsers;
//ddlInfoBox is a DropDownList to which I have Binded the collection of users in the form of string
SPUser user = userscollection.Web.EnsureUser(this.ddlInfoBox.SelectedValue);
SPFieldUserValue userval = new SPFieldUserValue(user.ParentWeb, user.ID, user.LoginName);
return userval;
}
set
{
SPFieldUserValue userval = (SPFieldUserValue) this.ItemFieldValue;
this.ddlInfoBox.SelectedValue = userval.Lookupvalue; //Here look up value is nothing but a Login name e.g In-Wai-Svr2\tjagtap
}
}
Due to above property the Custom Field's Value for this current ListItem will be stored as SPFieldUserValue e.g 27#;In-Wai-Svr2\tjagtap.
The main problem is here, when this particular ListItem is shown in the list page views e.g on AllItems.aspx or the custom view pages associated with it, it shows the
number as 27 as a FieldValue insted of HyperLink with text as "In-Wai-Svr2\tjagtap" and PostBackURL as "/_layouts/userdisp.aspx?ID=27".
When I edit this Item it makes the respective value selected in the dropdownlist, also while viewing this item i.e on DispForm.aspx it also shows the hyperlink. I have
acheived it by writting a custom logic in createchildcontrol() method i.e by using ControlMode if it is New or Edit then fill the dropdown list, if it is Display then get the ItemFieldValue Type Cast it into SPFieldUserValue and get corresponding lookupid and value for making the URL and showing Text of the HyperLink.
I have spent a lot of time on searching and bringing the HyperLink as the user name with navigation insted of UserID (27) as a string on the list view pages e.g AllItem.aspx but to no avail, then after a lot of research I found that there might be a way of achieving such kind of functionality by using field type definition xml file where there is a provision to define a DisplayPatteren as you wish by specifying the html code. But here is a problem How can I get the UserID (27) with respective UserName e.g In-Wai-Svr2\tjagtap inorder to make an anchor tag like In-Wai-Svr2\tjagtap which will solve my problem. I have hard coded this anchor tag within the Default case statement of a switch under DisplayPatteren but it shows me the field value on AllItems.aspx as
In-Wai-Svr2\tjagtap27 i.e the value defined in xml file is concatenating with the string value (27).
Please help me to resolve the above mentioned 2 issue. I am really in need of solving this problem ASAP.
Thanks & Regards,
Tejas Jagtap
Have u tried to override the GetFieldValueAsHtml() method in the the custom field class or maybe the RenderFieldForDisplay() method in the custom field control class.
Can you use the DisplayPattern CAML from the User field type?

Sharepoint default list item detail view

I have a webpart that renders random list items (from any list and list type) in a specified format. I want the items that are being displayed in the webpart to link to their ListItem detail views. However, I don't see a property on the list itself that would tell me what view is the default DETAIL view for the list (ie. blog list detail is Post.aspx). Does this come from the list definition? How would I get that information programmatically? I'm trying to avoid hard-coding any list information based on list type.
Have a look at SPList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url.
I think this is what you're looking for. You'll need to append the the SPListItem.ID on the querystring so that it knows which list item to display.
using (SPWeb myWeb = GetMyWeb()) // GetMyWeb gets a reference to a SPWeb object
{
SPList myList = GetMyList(myWeb); // GetMyList gets a reference to a SPList object
SPListItem myItem = GetMyListItem(myList); // GetMyListItem gets a reference to a SPListItem object
string url = String.Format("{0}/{1}?ID={2}",
myWeb.Url,
myList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url,
myItem.ID);
}
It's also a good practice to append &Source=/url/to/current/page to the querystring so that users will be redirected back to the page they left when they click the Cancel/Close buttons on the Edit or Display forms.

Resources