Mvc dropdownlist default value and placeholder - text

I have a html.dropdownlist which have 4 options. I want it to display a placeholder text a defaut which is not included in the options. I implemented a similar thing with adding a default option to the list, however selected default option is causing div to expand and it distrups the page layout.
<div style="float:left; padding-left:13.4%;">
#Html.DropDownList("Adults", ViewData["Adults"] as List<SelectListItem>, new { #class = "dropdown2"})
</div>
And that is the controller code;
List<SelectListItem> adt = new List<SelectListItem>();
//adt.Add(new SelectListItem
//{
// Text = "1",
// Value = "1"
//});
adt.Add(new SelectListItem
{
Text = "1",
Value = "1"
});
adt.Add(new SelectListItem
{
Text = "2",
Value = "2"
});
adt.Add(new SelectListItem
{
Text = "3",
Value = "3"
});
adt.Add(new SelectListItem
{
Text = "4",
Value = "4"
});
ViewData["Adults"] = adt;
return View();
Im new with mvc and any contribution will be appreciated. Thank you.

Adding placeholder text when its not directly implemented is tricky if not impossible. I recommend using Chosen (JQuery Plugin) instead. It is very easy to use and makes the dropdown beautiful.
https://harvesthq.github.io/chosen/
Example of a dropdown with chosen:
#Html.DropDownListFor(model => model.Example, Model.Example, new { #class = "chosen", data_placeholder="Choose items"})

You can add jquery code to add option to the dropdownlist with the select attribute so the option will be set as a placeholder:
$('#selectid').append($('<option/>', {
value: value,
text : value
}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select id='selectid'>
<option value="" disabled selected>Select your option</option>
<option value="hurr">Durr</option>
</select>

If you are using ASP.NET MVC 5 then you can use overloaded method of #Html.DropDownList
with perameter type string optionLabel.
which will add a new option in select list at first index. will be default selected. and if your default options always selected then no need for placeholder.
#Html.DropDownList("Adults", ViewData["Adults"] as List<SelectListItem>, "Select Adult",new { #class = "dropdown2"})

Related

How to change React text area value

I have a list that takes values from server and i want that whenever an item of the list gets clicked it will change a textarea text to the appropriate variable on the list,
I am getting the user using redux:
function ProfilePage() {
const dispatch = useDispatch();
const authObj = useSelector(state => state.auth);
const { user, token, expiredAt } = authObj;
I am generating the list like this:
<ul className="SentencesList">
{
user.approvedPatterns.map((el, i) => (
<div key={i}>
<li>{el.name || ''}</li>
<hr className="profilePageSentenceListHr" />
</div>
))
}
</ul>
And what i want is that when a li on the list get clicked i will take the appropriate value in the JSON object and show i
<textarea rows="5" cols="60" className="centerPageLargeInputBox" type="text" />
The JSON list looks like that:
const userList = [
{
_id: "1",
username: "****",
password: "*",
approvedPatterns:[
{
"name":"title 1",
"text":"some text"
},
{
"name":"title 2",
"text":"some text1"
},
{
"name":"title 3",
"text":"some text 2"
}
]
}
]
How can i accomplish that?
I am really lost.
I have created the demo here: https://stackblitz.com/edit/react-f5q3n8?file=src/App.js
So when anyone clicks on the li, the textarea will show the value of that li's text property.

How do I get a Kendo DatePicker to work inside an MVC Partial?

Index.cshtml
#Html.Partial("_DateFieldPartial", field);
_DateFieldPartial.cshtml
#model Simplifyed.Enrollment.Web.Contracts.GenericFormField
#using Kendo.Mvc.UI;
#{
ViewBag.Title = "_DateFieldPartial";
}
<div>
#(Model.FieldLabel):#(Html.Kendo().DatePicker()
.Name("datepicker")
.Value("10/10/2011")
.HtmlAttributes(new { style = "width: 100%", title = "datepicker", id = "123" })
.Deferred()
)
</div>
All I get is a text box, nothing to click. When I click the text box, nothing happens.
I am not using the NuGet package, I am only referencing the Kendo.MVC.dll
In Your layout did you add
#Html.Kendo().DeferredScripts()
Like specified on
https://docs.telerik.com/aspnet-mvc/getting-started/fundamentals#configuration-Deferring

Get Attribute Values From Child Element With No ID - JQuery

Lets say I have this code:
<div id="Element_1" class="draggable">
<input type="text" placeholder="Enter Your Text Here" style="width:300px;">
</div>
<div id="Element_2" class="draggable">
<textarea placeholder="Enter Your Text Here" style="width:400px;height:200px;"></textarea>
</div>
What I am trying to do is get the element attribute values, from the child of the "div" and also the "Type" (Input/Tagname) so I can store the values in variables.
Currently I can get the element and store in a Var, this is the code I have:
$(".draggable").live("click",function(){
var SelectedID = $(this).attr("id");
$("*").removeClass("selected");
var Element = $("#"+SelectedID,":first-child").addClass("selected").html();
PlaceholderPropValue = $(element).attr("placeholder");
StylesPropValue = $(element).attr("style");
WidthProp = StylesProp.match(/(width:)([0-9].*?)(?=px;)/)[2];
HeightProp = StylesProp.match(/(height:)([0-9].*?)(?=px;)/)[2];
alert(PlaceholderPropValue+ " " +WidthProp+ " " +HeightProp);
});
Thanks!
Carl
Your code is kind of overkill.
.live() is deprecated in jQuery 1.7 and totally removed in 1.9, jquery documentation says you should use .on() instead.
Everything is simple. You can try something like:
$('.draggable').click(function(){
var $element = $(this).children();
var width = parseInt($element.width());
var height = parseInt($element.height());
var placeholder = $element.attr('placeholder');
alert(placeholder+ " " +width+ " " +height);
})
.children() method normally return set of element's children but there are only one in your example, so it's ok.
.width() and .height() returns values like "300px", so we use parseInt() to make int values.
.click(handler) is a shortcut for .on( "click", handler )

Orchard cms Extending Menu Item part

What's the best way to extent the Menu part in orchard ?
I want to use the normal menu part for most content types.
But I also want a Custom MainNavigationMenuPart. that has all the things the menu part has but adds a media picker field and a text field to it. - These items that will display on menu rollover.
Option 1
I think this is the best way to go ...
I've looked at writing a custom Menu part - but it seem like a lot of functionality for how the menu part currently work is there, I'm not sure how best to tap into this in a DRY way.
I can add a MenuPartItem to my customPart, so main menu part model would look like this
public class MainMenuPart : ContentPart<MainMenuRecord>
{
public MenuPart MenuPart { get; set; }
public string ShortDescription
{
get { return Record.ShortDescription; }
set { Record.ShortDescription = value; }
}
}
But ...
how do I render this on in the editor view for the part ?
I want to use the exiting MenuPartItemEditor.
how do I save this information in the record for the part?
Option 2
I've looked also at adding fields to the menu part (via cms).
My menu part now looks like this on the back end
Here I have customise the admin view for the menu depending on the content type.
Buy creating a Parts.Navigation.Menu.Edit.cshtml in Views/EditorTemplates, in my custom them I have access to the menu part, but I can seem to control the display of the fileds I have added to the part. (menu image, highlight, and short description)
Here is the custom Parts.Navigation.Menu.Edit.cshtml (original found in Orchard.Core/Navigation/Views/EditorTemplates/Parts.Navigation.Menu.Edit.cshtml)
#model Orchard.Core.Navigation.ViewModels.MenuPartViewModel
#using Orchard.ContentManagement
#using Orchard.Core.Navigation.Models;
#if (!Model.ContentItem.TypeDefinition.Settings.ContainsKey("Stereotype") || Model.ContentItem.TypeDefinition.Settings["Stereotype"] != "MenuItem")
{
if (Model.ContentItem.ContentType == "StandardIndexPage" ||
Model.ContentItem.ContentType == "AlternateIndexPage" ||
Model.ContentItem.ContentType == "MapIndexPage")
{
var sd = ((dynamic)Model.ContentItem).MenuPart.ShortDescription;
#sd
<fieldset>
#Html.HiddenFor(m => m.OnMenu, true)
#Html.HiddenFor(m => m.CurrentMenuId, Model.CurrentMenuId)
<div>
<label for="MenuText">#T("Menu text (will appear on main menu)")</label>
#Html.TextBoxFor(m => m.MenuText, new { #class = "text-box single-line" })
<span class="hint">#T("The text that should appear in the menu.")</span>
</div>
</fieldset>
}
else
{
<fieldset>
#Html.EditorFor(m => m.OnMenu)
<label for="#Html.FieldIdFor(m => m.OnMenu)" class="forcheckbox">#T("Show on menu")</label>
<div data-controllerid="#Html.FieldIdFor(m => m.OnMenu)" class="">
<select id="#Html.FieldIdFor(m => m.CurrentMenuId)" name="#Html.FieldNameFor(m => m.CurrentMenuId)">
#foreach (ContentItem menu in Model.Menus)
{
#Html.SelectOption(Model.CurrentMenuId, menu.Id, Html.ItemDisplayText(menu).ToString())
}
</select>
<span class="hint">#T("Select which menu you want the content item to be displayed on.")</span>
<label for="MenuText">#T("Menu text")</label>
#Html.TextBoxFor(m => m.MenuText, new { #class = "text-box single-line" })
<span class="hint">#T("The text that should appear in the menu.")</span>
</div>
</fieldset>
}
}
else
{
<fieldset>
<label for="MenuText">#T("Menu text")</label>
#Html.TextBoxFor(m => m.MenuText, new { #class = "textMedium", autofocus = "autofocus" })
<span class="hint">#T("The text that should appear in the menu.")</span>
#Html.HiddenFor(m => m.OnMenu, true)
#Html.HiddenFor(m => m.CurrentMenuId, Request["menuId"])
</fieldset>
}
I've also tried to control the display of fields using the placement.info in the theme
<Match ContentType="StandardIndexPage">
<Place Fields_Boolean_Edit-Highlight="-"/>
</Match>
with no success.

Drupal - Search box not working - custom theme template

I am using a customised version of search-theme-form.tpl
When I use the search box, I do get transferred to the search page. But the search does not actually take place. The search box on the search results page does work though. This is my search-them-form.tpl.php file (demo :
<input type="text" name="search_theme_form_keys" id="edit-search-theme-form-keys" value="Search" title="Enter the terms you wish to search for" class="logininput" height="24px" onblur="restoreSearch(this)" onfocus="clearInput(this)" />
<input type="submit" name="op" id="edit-submit" value="" class="form-submit" style="display: none;" />
<input type="hidden" name="form_token" id="edit-search-theme-form-form-token" value="<?php print drupal_get_token('search_theme_form'); ?>" />
<input type="hidden" name="form_id" id="edit-search-theme-form" value="search_theme_form" />
There is also a javascript file involved. I guess it's use is pretty clear from the code:
function trim(str) {
return str.replace(/^\s+|\s+$/g, '');
}
function clearInput(e) {
e.value=""; // clear default text when clicked
e.className="longininput_onfocus"; //change class
}
function restoreSearch(e) {
if (trim(e.value) == '') {
{
e.value="Search"; // reset default text onBlur
e.className="logininput"; //reset class
}
}
}
What can be the problem and how can I fix it?
Apparently, you cannot directly modify the HTML in search-theme-form.tpl.php since thats not the right way to do it. So my adding the class and onFocus and onBlur attributes was the problem.
The correct way to do it is to modify the themes template.php file. Basically we will be using form_alter() to modify the form elements. Since using the HTML way is wrong. Take a look at the code below (taken from : here )
<?php
/**
* Override or insert PHPTemplate variables into the search_theme_form template.
*
* #param $vars
* A sequential array of variables to pass to the theme template.
* #param $hook
* The name of the theme function being called (not used in this case.)
*/
function yourthemename_preprocess_search_theme_form(&$vars, $hook) {
// Note that in order to theme a search block you should rename this function
// to yourthemename_preprocess_search_block_form and use
// 'search_block_form' instead of 'search_theme_form' in the customizations
// bellow.
// Modify elements of the search form
$vars['form']['search_theme_form']['#title'] = t('');
// Set a default value for the search box
$vars['form']['search_theme_form']['#value'] = t('Search this Site');
// Add a custom class and placeholder text to the search box
$vars['form']['search_theme_form']['#attributes'] = array('class' => 'NormalTextBox txtSearch',
'onfocus' => "if (this.value == 'Search this Site') {this.value = '';}",
'onblur' => "if (this.value == '') {this.value = 'Search this Site';}");
// Change the text on the submit button
//$vars['form']['submit']['#value'] = t('Go');
// Rebuild the rendered version (search form only, rest remains unchanged)
unset($vars['form']['search_theme_form']['#printed']);
$vars['search']['search_theme_form'] = drupal_render($vars['form']['search_theme_form']);
$vars['form']['submit']['#type'] = 'image_button';
$vars['form']['submit']['#src'] = path_to_theme() . '/images/search.jpg';
// Rebuild the rendered version (submit button, rest remains unchanged)
unset($vars['form']['submit']['#printed']);
$vars['search']['submit'] = drupal_render($vars['form']['submit']);
// Collect all form elements to make it easier to print the whole form.
$vars['search_form'] = implode($vars['search']);
}
?>
In yourthemename_preprocess_search_theme_form - 'yourthemename' will obviously reflect the name of your custom theme. Basically the code is self-explanatory. what with the comments and all.
So, basically thats the way it works.

Resources