Want to reduce my logic on View data from drop down won't come from data base.
Current code
#Html.DropDownListFor(m => m.MyViewModel.Value,
new List<SelectListItem> {
new SelectListItem { Text = "1",Value = "1" },
new SelectListItem { Text = "2", Value = "2" },
new SelectListItem { Text = "3", Value = "3" },
new SelectListItem { Text = "4", Value = "4" },
new SelectListItem { Text = "5", Value = "5" },
});
Trying to achieve
<select id="MyViewModel.name" name="MyViewModel.name">
<option value="">--Select--</option>
#for (int i = 1; i <= 5; i++)
{
<option value="#i">#i</option>
}
</select>
On submitting the form I am getting the selected value in my ViewModel i.e action but on editing am not the selected value Like
<select data-val="true" id="ddl" name="">
<option value="1">1</option>
<option value="2">2</option>
**<option selected="selected" value="3">3</option>**
<option value="4">4</option>
<option value="5">5</option>
</select>
You code for generating the SelectList can be simplified to
#Html.DropDownListFor(m => m.yourProperty, new SelectList(new List<int> { 1, 2, 3, 4, 5 }))
However, that code belongs in the controller, not the view, and ideally you should have a view model with property IEnumerable<SelectListItem>, for example
public class MyViewModel
{
[Required(ErrorMessage = "...."]
[Display(Name = "....")]
public int? SelectedItem { get; set; } // nullable to protect against under-posting attacks
public IEnumerable<SelectListItem> Options { get; set; }
....
}
and in the GET method
MyViewModel model = new MyViewModel
{
Options = new SelectList(new List<int> { 1, 2, 3, 4, 5 })
};
return View(model);
and in the view
Html.DropDownListFor(m => m.SelectedItem, Model.Options, "Please select")
it will be best to define the data at the control level than in Razor
so create and edit actions will return a list which will be used in DropDownListFor
but Edit will also set the selected value
Related
I'm trying to create a dropdown list and I get nothing in the dropdown except 'Please select one' but no values.
I've tried this so far:
View:
<select asp-for="StatusToEdit.Color.Name" asp-items="Model.AvailableColors">
<option>Please select one</option>
</select>
Model:
public SelectList AvailableColors { get; set; }
public void OnGet()
{
AvailableColors = new SelectList(nameof(StatusColor.ColorId),
nameof(StatusColor.Name));
}
Mocklist:
private static List<ItemStatus> _mockStatuses = new List<ItemStatus>
{
new ItemStatus { StatusId = 1, Name = "Complete", Color = new
StatusColor{ ColorId = 1, Name = "Auto" } },
new ItemStatus { StatusId = 2, Name = "Complete, Ongoing", Color =
new StatusColor{ ColorId = 2, Name = "Green" }},
new ItemStatus { StatusId = 3, Name = "In Process", Color = new
StatusColor{ ColorId = 3, Name = "Yellow" }}
};
I expect to see all of the colors to show in the dropdown.
if you are using asp.net core MVC (Razor Pages is similar), you need to modify the model property type to IEnumerable<SelectListItem> AvailableColors or List<SelectListItem> AvailableColors
1.Model
public class ManageStatusesEditViewModel
{
//other properties
public List<SelectListItem> AvailableColors { get; set; }
}
2.Action:
[HttpGet]
public IActionResult ManageStatusesEdit(int id)
{
//other logic
var dropdownData = new List<SelectListItem>();
_mockStatuses.ForEach(d => dropdownData.Add(new SelectListItem()
{
Value = d.Color.Name,
Text = d.Color.Name
}));
var editManageStatusesEditViewModel = new ManageStatusesEditViewModel
{
AvailableColors = dropdownData
};
return View(editManageStatusesEditViewModel);
}
3.View:
<select asp-for="StatusToEdit.Color.Name" asp-items="Model.AvailableColors">
<option>Please select one</option>
</select>
4.Result:
You should create your list as below:
ViewBag.ColorList = new SelectList(_mockStatuses.Select(d=> { return new SelectListItem { Text = d.Color.Name, Value = d.Color.ColorId.ToString() }; }),"Value","Text");
I used ViewBag for it but you can obviously pass it to your view by a ViewModel if you prefer.
For your View you should do something as below:
<select asp-items="#ViewBag.ColorList" asp-for="StatusToEdit.Color.Name" >
<option value="">Please select one</option>
</select>
Please make sure you have added Value and Text at the end of SelectList when creating it.
I am working on an asp.net application. I have a viewbag with a boolean result (true or false) and I need to select one option from a razore view using this viewbag. I have done it like this.
Controller:
ViewBag.AMZ = result.shipment.ShipAMZ ;
View:
#{
var IsAmzSelected = (bool)ViewBag.AMZ;
}
<select id="amz" name="amz" class="form-input">
#if (IsAmzSelected == true)
{
<option value='1' selected>AMZ</option>
<option value='0'>BBW</option>
}
else
{
<option value='1'>AMZ</option>
<option value='0' selected>BBW</option>
}
</select>
I tried using Html dropdownlist helper method but it didn't work.
in controller i did it like this:
ViewBag.AMZ = new SelectList(new List<SelectListItem>() { new SelectListItem() { Text = "AMZ", Value = "1" }, new SelectListItem() { Text = "BBW", Value = "0",Selected=true } }, "Value", "Text" , result.shipment.amz);
and then bound it to htmldropdown to this viewbag but it didn't work
Can you please suggest how to do it using html.dropdown?
I am populating a dropdown using as
[Required(ErrorMessage = "Please make a selection")]
public string SelectedPrimary { get; set; }
public IEnumerable<SelectListItem> PrimaryDrop { get; set; }
public void populateDropdown()
{
primaryDrop = new List<string>();
primaryDrop.Insert(0, "Getting ready");
primaryDrop.Insert(1, "Starting");
primaryDrop.Insert(2, "All");
PrimaryDrop = primaryDrop.Select(item => new SelectListItem { Value = item, Text = item });
}
And then my razor view is as follow
#Html.DropDownListFor(m => m.SelectedPrimary, new SelectList(Model.PrimaryDrop, "Value", "Text"), "Learning Path", new { style = "width:207px;", id = "FirstDropDown" })
Upon inspecting the element I would see this
<select data-val="true" data-val-required="Please make a selection" id="FirstDropDown" name="SelectedPrimary" style="width:207px;">
<option value="">Learning Path</option>
<option value="Getting ready">Getting ready</option>
<option value="Starting">Starting</option>
<option value="All modules">All</option>
</select>
How could I go about in setting the values to the index ?
<select data-val="true" data-val-required="Please make a selection" id="FirstDropDown" name="SelectedPrimary" style="width:207px;">
<option value="0">Learning Path</option>
<option value="1">Getting ready</option>
<option value="2">Starting</option>
<option value="3">All modules</option>
</select>
You could just use a Dictionary to build the SelectList. Then use key as the value and value as the text
var primaryDrop = new Dictionary<string, string>() {
{"0", "Getting Ready"},
{"1", "Starting"},
{"2", "All"}
};
PrimaryDrop = primaryDrop.Select(item =>
new SelectListItem { Value = item.Key, Text = item.Value });
You could fill PrimaryDrop like this (using List<T>.IndexOf(T) to get Value)
primaryDrop = new List<string>();
primaryDrop.Insert(0, "Getting ready");
primaryDrop.Insert(1, "Starting");
primaryDrop.Insert(2, "All");
PrimaryDrop = primaryDrop.Select(item => new SelectListItem
{
Value = primaryDrop.IndexOf(item),
Text = item
});
My model:
public class ViewRequestModel
{
[Required(ErrorMessage = "some")]
[Display(Name = "some")]
public int RequestType { get; set; }
}
my controller:
[HttpPost]
[Authorize(Roles = "P")]
public PartialViewResult ViewRequests(ViewRequestModel model)
{
string vn = "";
switch (model.RequestType)
{
...
}
return PartialView(vn);
}
my view:
#{
var reqTypes = new List<ListItem> { new ListItem { Text = "t1", Value = "1" },
new ListItem { Text = "t2", Value = "2" },
new ListItem { Text = "t3", Value = "3" } };
}
#Html.DropDownListFor(model => model.RequestType, new SelectList(reqTypes), new { id = "ddlType" })
#Html.ValidationMessageFor(model => model.RequestType)
when I try to post my form, jquery validation blocks it and show the error The field RequestType must be a number
where's my mistake?
where's my mistake?
The fact that you are mixing some classic WebForms classes (ListItem) with ASP.NET MVC. The consequence of this is that your <option> elements of the dropdown do not have a value attribute. So nothing gets submitted to the form and the validation obviously fails.
Here's the correct way to populate the dropdown:
var reqTypes = new[]
{
new SelectListItem { Text = "t1", Value = "1" },
new SelectListItem { Text = "t2", Value = "2" },
new SelectListItem { Text = "t3", Value = "3" }
};
#Html.DropDownListFor(
model => model.RequestType,
reqTypes,
new { id = "ddlType" }
)
As you can see from this example I am using the SelectListItem class which is specific for ASP.NET MVC. This generates:
<select data-val="true" data-val-number="The field some must be a number." data-val-required="some" id="ddlType" name="RequestType">
<option selected="selected" value="1">t1</option>
<option value="2">t2</option>
<option value="3">t3</option>
</select>
wheres your code generates:
<select data-val="true" data-val-number="The field some must be a number." data-val-required="some" id="ddlType" name="RequestType">
<option>t1</option>
<option>t2</option>
<option>t3</option>
</select>
The difference is obvious.
I have a list of Organizations (CalledOrganizationSelectList) and one of them is the one that's being called (CalledOrganizationId). I set both in my ViewModel but MVC / Razor doesn't render the dropdown correctly (ie does not set the selected="selected" attribute for the correct item in the dropdown).
There is a workaround which makes even less sense. I added a new member to my ViewModel and bound the dropdown to that. This works fine. Until just now when it's stopped working...
public class CallViewModel{
public int? CalledOrganizationId { get; set; }
public SelectList CalledOrganizationSelectList { get; set; }}
In my controller:
var vm = new CallViewModel();
var calledOrganizationSelectList = new List<object>();
calledOrganizationSelectList.Add( new {Text="",Id=""});
calledOrganizationSelectList.AddRange(
db.MyOrganizations.OrderBy(x=>x.Name)
.Select(x => new { Text = x.Name, Id = x.Id.ToString()}).ToList());
var sl = new SelectList(calledOrganizationSelectList, "Id", "Text",
vm.CalledOrganizationId);
vm.CalledOrganizationSelectList = sl;
return View(vm);
In my view:
<div>CalledOrganizationId = #Model.CalledOrganizationId :
select list contains
<ul>#foreach (var itm in Model.CalledOrganizationSelectList)
{<li>Value: #itm.Value Text: #itm.Text Is Selected: #itm.Selected</li>}
</ul>
</div>
#Html.DropDownList("CalledOrganizationId", Model.CalledOrganizationSelectList)
In my rendered page source:
<div>CalledOrganizationId = 38 : select list contains
<ul>
<li>Value: Text: Is Selected: False</li>
<li>Value: 37 Text: rrr Is Selected: False</li>
<li>Value: 38 Text: sss1 Is Selected: True</li>
</ul>
</div>
<select data-val="true" data-val-number="The field CalledOrganizationId must be a number." id="CalledOrganizationId" name="CalledOrganizationId">
<option selected="selected" value=""></option>
<option value="37">rrr</option>
<option value="38">sss1</option> </select>
I've worked around it after first pulling out what was left of my hair and then by introducing a new variable on my ViewModel, which seems to work ok.
Having 2 properties on your viewmodel is the correct way to do DropDownLists. One property holds all of the available options, and the other holds the currently selected option:
public class CallViewModel
{
// this captures the selected item
public int? CalledOrganizationId { get; set; }
// this contains the select list options
public IEnumerable<SelectListItem> CalledOrganizationSelectList { get; set; }
}
However, you do not need to add the empty option in the controller. You can do this in the view:
#Html.DropDownListFor(m => m.CalledOrganizationId,
Model.CalledOrganizationSelectList, "")
You can just get away with this in the controller:
var vm = new CallViewModel();
//var calledOrganizationSelectList = new List<object>();
//calledOrganizationSelectList.Add( new {Text="",Id=""});
//calledOrganizationSelectList.AddRange(
//db.MyOrganizations.OrderBy(x=>x.Name)
// .Select(x => new { Text = x.Name, Id = x.Id.ToString()}).ToList());
//var sl = new SelectList(calledOrganizationSelectList, "Id", "Text",
// vm.CalledOrganizationId);
//vm.CalledOrganizationSelectList = sl;
vm.CalledOrganizationSelectList = db.MyOrganizations.OrderBy(x => x.Name)
.Select(x => new SelectListItem
{
Text = x.Name,
//Id = x.Id.ToString() // do not use "Id" for the property name,
Value = x.Id.ToString() // use "Value" -- it is a SelectListItem property
});
// if you want to set the selected item on the dropdownlist, do it here
vm.CalledOrganizationId = 37;
return View(vm);