I have a dropdown list that populates with two fields. Old_ItemID and ItemDescription. The user needs to see both of these fields in the dropdown.
Problem is that is messy. Originally it puts both fields next to each other without any spacing. It looks messy and is hard on the eyes. As you can see from the code below, I added some spacing. And that is much better.
The problem now is that it's not aligned because each Old_ItemID is a different length and depending on how many characters the Old_ItemID is, it pushed the ItemDescription left or right. And so it looks unorganized.
Is there a way to justify these columns in a tab setting, or somehow get the alignment correct?
Controller Code
List<SelectListItem> SuppliesList = db.ICS_Supplies.Where(s => s.InvType == "G").Select(x => new SelectListItem { Value = x.Supplies_ID.ToString(), Text = x.Old_ItemID + "\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0" + " | " + " " + " Description: " + x.ItemDescription, Selected = false }).DistinctBy(p => p.Text).OrderBy(p => p.Text).ToList();
ViewBag.SuppliesList = new SelectList(SuppliesList, "Value", "Text");
View Code
<div class="form-group">
#Html.Label("Supplies Requested:", new { #class = "form-control" })
#Html.DropDownList("SuppliesList", null, "Select", new { #class = "form-control", #onchange = "supplychange()" })
</div>
Note: I did a bit of research before asking this question, which is where I found how to add the spaces. But I can't seem to find a resource to show how to align with proper spacing so that it looks professional and not sloppy. Should be a common request, I would imagine?
Related
I am trying to add a drop down select element to my .net page, but I need it to be black so I can populat it with a json list I have with jQuery. Right now, my view has this:
#Html.DropDownList("Language", new SelectList(" "), "name = 'Laguange'", new { #class = "Language-List field form-group form-control" })
This 'works', but it seems to be confusing some stuff when I try to submit the form it is in. The value I select from it is never seen by the model, as it keeps triggering my [Required] validation helper.
Does anyone know How to add this without the new SelectList(" ") attribute?
I would suggest it's because youre using the name attribute in the wrong place.
Try this:
#Html.DropDownList("Language", new SelectList(" "), new {Name="Language", #class = "Language-List field form-group form-control" })
Notice the capital Name
Although as they are the same, you can just leave it out:
#Html.DropDownList("Language", new SelectList(" "), new {#class = "Language-List field form-group form-control" })
<div class="input-group" style="margin-bottom:30px;">
<span class="input-group-addon">
Brand
</span>
#Html.DropDownListFor(x => x.BrandMod.Id, c,"- Select Brand - ", new { #class = "form-control" ,id="brandid" })
</div>
"This is My Code for Drop Down and I want to select the selected item. I am trying this"
$("#brandid").change(function (rupp) {
var a = $(this).select("option:selected").val();
});
"I am Unable to Load the Selected item."
This doesn't do what you're thinking:
$(this).select("option:selected").val()
In fact, it's probably either silently failing entirely or producing an error. But no matter. What you're trying to do is get the value from this (which is a <select> in your case). That can be done simply with:
$(this).val()
You don't need Specification for getting value in jquery. You have already change event for this
$("#brandid").change(function (rupp) {
var a = $(this).val();
});
or
var a = $("#brandid").val();
I have an html dropdownlistfor which is being populated from my database, this I was able to do successfully but the issue I am having now is including an additional option not available in the database as an option that can also be selected.it is actually a list of items but I want a give an option ALL which when selected means the user is selecting all the items displayed in the database, my major issue is how to include the ALL option. I have searched and read similar questions but they are not what am trying to achieve. thanks for your help. here is my code
in my controller I have this:
var load = from bh in db.IV_001_ITEM
select bh;
ViewBag.selection = new SelectList(load.ToList(), "item_code", "item_name", glay.vwstring2);
in my view I have this:
<div class="col-sm-2">
#Html.DropDownListFor(m => m.vwstring2, ViewBag.selection as SelectList, "Select", new { #class = "form-control", required = "required", id = "selectc" } )
</div>
You can use the Add method to explicitly add an extra option (SelectListItem)
List<SelectListItem> optionList = db.IV_001_ITEM
.Select(x=>new SelectListItem { Value=x.item.code,
Text=x.itemName}).ToList();
// Now add the item you want
optionList.Add(new SelectListItem { Value="Foo",Text="Bar"});
//use this now
ViewBag.Items = optionList;
// You can set the selected item on your view model property
myViewModelObject.vwstring2 = glay.vwstring2; //For the selected item
return View(myViewModelObject);
In your view,
#Html.DropDownListFor(m => m.vwstring2, ViewBag.Items as List<SelectListItem>,
"Select", new { #class = "form-control"})
I am trying to modify a behavior for DataAttributes (Maxlength[int] or StringLength[int]), where it would add a "maxlength" attribute inside (textarea) tag along with its "int" value. I have already performed some research, and the closest answers I obtained were this post and that question.
However, I hope to go one step further.
I already know that StringLength adds "data-val-length-max" and "data-val-length" attributes to the tag.
I spent a lot of time trying to implement solution in JavaScript unobtrusive validation file by using logic
if tag has data-val-length-max attribute
add maxlength attribute to this tag
assign its value equal to data-val-length-max value
However, in my project, Javascript files are constantly loaded/unloaded and it is extremely time-consuming to keep track of the logic flow, so I decided to change the approach, and try to implement it within C#.
Inside my .cshtml file, there are lines like following:
#Html.myLabelFor(model => model.Project.ProjectDescription, new { #title = "Enter a description for this project" })
#Html.TextAreaFor(model => model.Project.ProjectDescription, new { #class = "textfield_Paragraph", #id = "ProjectDescription" })
#Html.ValidationMessageFor(model => model.Project.ProjectDescription)
I learned that it successfully adds maxvalue if it adds #maxlength at the end:
#Html.TextAreaFor(model => model.Project.ProjectDescription, new { #class = "textfield_Paragraph", #id = "ProjectDescription", #maxlength = 50 })
However, I do not want to manually add #maxlenght this line to every TextAreaFor line in solution; I want to turn it into reusable code, that would dynamically perform such action to every variable within TextAreaFor inside the .cs file, which are marked like following:
[MaxLength(50)]
[Display(Name = "Project Description")]
public string ProjectDescription { get; set; }
So I am thinking of implementing it by using the logic
if metadata has a MaxLength flag
htmlAttributes += " ', #maxlength = ' + maxLengthValue"
So, physically it will still be
#Html.TextAreaFor(model => model.Project.ProjectDescription, new { #class = "textfield_Paragraph", #id = "ProjectDescription" })
while the final value that will be loaded will be
#Html.TextAreaFor(model => model.Project.ProjectDescription, new { #class = "textfield_Paragraph", #id = "ProjectDescription", #maxlength = 50 })
I want to know if something like this is even possible to implement, so I know beforehand if I should continue thinking in that direction.
Again, links I have posted above were the closest answers I could get, so I am curious if it is as far as I can get, or there is even more room to improve. My huge preference would be NOT touching .js files unless I absolutely HAVE to.
It seems impossible and I have tried a few different ways to create a select list and bind a value to it.
Firstly, and more desirably, I want to do it with Razor:
#Html.DropDownList("SelectedTemplate", Model.Templates, new { #data_bind = "value: Template()" })
The options are just an array of strings. When I bind with Razor then it's a List of SelectListItem objects with Text and Value equal to the same string.
So it renders like this:
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
When I bind via Knockout then the Templates is an array of strings:
var Templates ='["A", "B", "C"]';
which I generate in the cshtml file:
var Templates ='#Html.Raw(Json.Encode(Model.Templates.Select(x=> x.Value)))';
Both create the same markup in HTML. This renders the select list correctly. Template() prints out the correct value, for example "B". However, the drop down doesn't pick the option that is equal to Template(). I also tried:
<select name="SelectedTemplate" data-bind="value: Template(), options: JSON.parse(Templates)"></select>
Same problem here. And I tried all kinds of variations:
#Html.DropDownListFor(m => m.SelectedTemplate, Model.Templates, new { #class = "form-control layoutTemplateSelector", #data_bind = "value: Template(), options: JSON.parse(Templates)" })
#Html.DropDownListFor(m => m.SelectedTemplate, Model.Templates, new { #class = "form-control layoutTemplateSelector", #data_bind = "value: Template()" })
#Html.DropDownList("SelectedTemplate", Model.Templates, new { #data_bind = "value: Template(), options: JSON.parse(Templates)" })
Ideally, I want to use DropDownListFor because on post back I want m.SelectedTemplate to be processed.
The postback works well so far for DropDownListFor, but on load it doesn't show the data-bind: value:Template(), which definitely contains the correct value.
I also tried your idea to use observable array with no success:
self.SelectedContent({
Template: ko.observable(content.Template()),
TemplateList: ko.observableArray(JSON.parse(Templates))
});
#Html.DropDownListFor(m => m.Form.Template, Model.Templates, new {#data_bind = "value: Template(), options: TemplateList()" })
Any ideas what could be wrong here? I'm using Knockout 2.3 and MC.
UPDATE
I found the error.
The value returned from the database contained a space: "A "
whereas the values binding the to the drop down didn't have that space.
This caused all the errors.
As far as I can see the simplest of implementations works well now:
#Html.DropDownList("SelectedTemplate", Model.Templates, new { #data_bind = "value: Template()" })
Even though it was entirely my mistake, I hope it helps someone in the future. The error wasn't immediately apparent