I am binding a dropdownlist using the below code
Model
public IEnumerable<TitleNew> TitleOption =
new List<TitleNew>
{
new TitleNew {Id = 0, Value = "---Select---"},
new TitleNew {Id = 1, Value = "Mr."},
new TitleNew {Id = 2, Value = "Ms."}
};
public class TitleNew
{
public int Id { get; set; }
public string Value { get; set; }
}
public string Title { get; set; }
View
#Html.DropDownListFor(m => m.Title,
new SelectList(Model.TitleOption,
"Id", "Value",
Model.TitleOption.First().Id))
I was unable to set the selected value to dropdownlist. The selected value comes from the database and it sets for Title property.
Please help me out.
new TitleNew {Id = 0, Value = "---Select---"} looks like an option label, which is already included in an overloaded method for dropdownlistfor.
So if you want that to be the option that all users first see when they look at the dropdownlist then this should work:
public IEnumerable<TitleNew> TitleOption =
new List<TitleNew>
{
new TitleNew {Id = 0, Value = "Mr."},
new TitleNew {Id = 1, Value = "Ms."}
};
public class TitleNew
{
public int Id { get; set; }
public string Value { get; set; }
}
public string Title { get; set; }
View:
#Html.DropDownListFor(m => m.Title, new SelectList(Model.TitleOption, "Id", "Value"), "-- Select -- ")
Related
I am having problem adding to the model. I want to have a list in the Root1 or in the Viewmodel like fullname and authors list.
newModel1
AuthorsAccepted count=4
FullName = Jack
AuthorsAccepted count=4
FullName = Time Dean
With the code below it only adds the last one to the Root1. I created a a third model. Is there a way I can add to the third model both the lists or in Root1.
var fullName = "Jack";
var fullName2 = "Tim Dean";
var authors1 = new List<AuthorsAccepted1>() {
new AuthorsAccepted1(){ id = 1, Name="Bill"},
new AuthorsAccepted1(){ id = 2, Name="Steve"},
new AuthorsAccepted1(){ id = 3, Name="jon"},
new AuthorsAccepted1(){ id = 4, Name="nick"}
};
var authors2 = new List<AuthorsAccepted1>() {
new AuthorsAccepted1(){ id = 1, Name="jack"},
new AuthorsAccepted1(){ id = 2, Name="tim"},
new AuthorsAccepted1(){ id = 3, Name="james"},
new AuthorsAccepted1(){ id = 4, Name="mary"}
};
var newModel1 = new Root1();
newModel1.FullName = fullName;
newModel1.AuthorsAccepted = authors1;
var newModel2 = new Root1();
newModel2.FullName = fullName2;
newModel2.AuthorsAccepted = authors2;
}
}
public class Root1
{
public string FullName { get; set; }
public List<AuthorsAccepted1> AuthorsAccepted { get; set; }
}
public class AuthorsAccepted1
{
public string Name { get; set; }
public int id { get; set; }
}
public class Viewmodel
{
public Root1 AllModel { get; set; }
}
I have this MultipleSelectList in my view:
#Html.ListBoxFor(s => s.Id,
new MultiSelectList((IEnumerable<SelectListItem>)ViewData["ddlList"], "Value", "Text", Model.Id),
new { #style = "margin-top:250px", multiple = "multiple" })
This list is populated here
#{
using (var b = new Entity())
{
ViewData["ddlList"] = b.Table.Select(e => new SelectListItem()
{
Value = e.Id.ToString(),
Text = e.Name
}).ToList();
}
}
This is my model
public string Name { get; set; }
public int[] Id { get; set; }
The problem is that when I select multiple options, only the first one gets to my controller like this:
int[] value = modelObj.Id;
modelObj.Id;--stores my selected values
Someone knows how can I solve this?
Selected values will be array of string not array of int. So, change the type of Id property to type of string[]:
public string[] Id { get; set; }
I am trying to create one partial view to create drop down on every view. Have created one view model and partial view for the same. Its creating drop down properly but not showing selected value.
Below is View Model Class
public class drowDownVM
{
public string id { get; set; }
public string name { get; set; }
public string cssClass { get; set; }
public string keyColumnName { get; set; }
public string valueColumnName { get; set; }
public string selectedValue { get; set; }
public string viewbagName { get; set; }
public bool isMultipleSelect { get; set; }
public List<int> multipleselectedValue { get; set; }
}
Below is partial view to bind drop down
#if (Model != null)
{
if (ViewData != null && ViewData.Count > 0 && ViewData.Keys.Contains(Model.viewbagName))
{
string ddName = !(string.IsNullOrEmpty(Model.name)) ? Model.name : "default-name";
string viewBagName = !(string.IsNullOrEmpty(Model.viewbagName)) ? Model.viewbagName : ViewData.Keys.First();
string keyColumnName = !(string.IsNullOrEmpty(Model.keyColumnName)) ? Model.keyColumnName : "id";
string valueColumnName = !(string.IsNullOrEmpty(Model.valueColumnName)) ? Model.valueColumnName : "id";
string selectedVal = !(string.IsNullOrEmpty(Model.selectedValue)) ? Model.selectedValue : "";
List<int> multipleSelectVal = (Model.multipleselectedValue != null && Model.multipleselectedValue.Count > 0) ? Model.multipleselectedValue : new List<int>();
var cssClass = !(string.IsNullOrEmpty(Model.cssClass)) ? Model.cssClass : "";
if (!Model.isMultipleSelect)
{
<div>
#Html.DropDownList(ddName, new SelectList((System.Collections.IEnumerable)ViewData[viewBagName], keyColumnName, valueColumnName, 2), "--Select--", new { #class = cssClass, #data_Val = selectedVal })
</div>
}
else
{
#Html.ListBox(ddName, new MultiSelectList((System.Collections.IEnumerable)ViewData[viewBagName], keyColumnName, valueColumnName, multipleSelectVal), new { #class = cssClass, #multiple = "multiple"})
}
}
}
else
{
<p class="hidden">Model Data Not Found!</p>
}
Below is the code which calls partial view to bind drop down, first one is single select, second one is calling multiple select.
#Html.Partial("_dropdown", new drowDownVM() { cssClass = "form-control", id = "TargetTypeList", keyColumnName = "code_val", name = "TargetTypeList", selectedValue = "1", valueColumnName = "code_name", viewbagName = "TargetTypeList"})
#Html.Partial("_dropdown", new drowDownVM() { cssClass = "form-control", id = "TargetTypeList", isMultipleSelect = true, keyColumnName = "code_val", name = "TargetTypeList", selectedValue = "1", valueColumnName = "code_name", viewbagName = "TargetTypeList", multipleselectedValue = new List<int>() { 1, 2 } })
<div>
#Html.DropDownList(ddName, new SelectList((System.Collections.IEnumerable)ViewData[#ViewBag.ViewBagName], keyColumnName, valueColumnName, 2), "--Select--", new { #class = cssClass, #data_Val = selectedVal })
</div>
I'm trying to do this:
This is my ViewModel and Model:
public class OpeningYearViewModel
{
public int OpeningYearId { get; set; }
public string Description { get; set; }
public List<Grade> GradesList { get; set; }
}
public class Grade
{
public int GradeId { get; set; }
public string Name { get; set; }
public int CurrencyId { get; set; }
public int Cost { get; set; }
}
This is my Controller. I build a SelecList here and pass it to the view through the ViewBag
OpeningYearViewModel viewmodel = new OpeningYearViewModel {
OpeningYearId = 1,
Description = "2015 - II",
GradesList = new List<Grade>
{
new Grade { GradeId = 1, Name = "Grade 1", CurrencyId = 1, Cost = 100 },
new Grade { GradeId = 2, Name = "Grade 2", CurrencyId = 2, Cost = 200 },
new Grade { GradeId = 3, Name = "Grade 3", CurrencyId = 2, Cost = 150 }
}
};
SelectList list = new SelectList(
new List<SelectListItem>
{
new SelectListItem { Text = "S/.", Value = "1"},
new SelectListItem { Text = "$", Value = "2"},
}, "Value" , "Text");
ViewBag.currencyList = list;
return View(viewmodel);
And in my View I need a DropDownListFor for every item on GradesList so I do this:
#model Test.Models.OpeningYearViewModel
#for(int i = 0; i < Model.GradesList.Count; i++)
{
#Html.DropDownListFor(x => x.GradesList[i].CurrencyId, new SelectList(ViewBag.currencyList, "Value", "Text"))
#Model.GradesList[i].CurrencyId //This is just to know the CurrencyId on every item.
}
I'm getting every select correctly rendered, but I can't get the correct option selected on the page load:
render of view
It is possible to do what I'm trying to do and I'm doing something wrong, or DropDownListFor works in a different way?
Thanks!
I can't understand why this is happening but you can workaround it by setting explicitly the selected value. This can be done by passing Model.GradesList[i].CurrencyId as fourth parameter to the SelectList's constructor:
#for(int i = 0; i < Model.GradesList.Count; i++)
{
#Html.DropDownListFor(x => x.GradesList[i].CurrencyId,
new SelectList(ViewBag.currencyList, "Value", "Text", Model.GradesList[i].CurrencyId))
}
Since, the DropDownListFor is used in loop to generate indexed inputs; so generate input controls will be generated with name as "GradeList[0].CurrencyId", "GradeList[1].CurrencyId"......Due to this framework will not bind the selected value in Select List as it is unable to get the value in reflection for selection. That's why you have to use the following SelectList constructor to set the selected value.
public SelectList(
IEnumerable items,
string dataValueField,
string dataTextField,
object selectedValue
)
However, if your DropDownListFor is bind to a simple expression like
#Html.DropDownListFor(model => model.CurrencyId, new SelectList(ViewBag.Items, "Value", "Text"))
then selection will work automatically.
So im working with this complex anonymous object
var result = new
{
percentage = "hide",
bullets = (string)null,
item = new [] {
new {
value = 16,
text = "Day",
prefix = (string)null,
label = (string)null
},
new {
value = 41,
text = "Week",
prefix = (string)null,
label = (string)null
},
new {
value = 366,
text = "Month",
prefix = (string)null,
label = (string)null
}
}
};
And I want to convert it into a ViewModel and return it as JSON from a rest API.
What I would like to know is
How do I represent this as a model including the array item entries
How do I then add array items to the array once an instance of the model is created
Does the model need a constructor to initialize the array.
Any help or examples you could provide would be great.
Create a class with the structure:
public class Result
{
public Result()
{
// initialize collection
Items = new List<Item>();
}
public string Percentage { get; set; }
public string Bullets { get; set; }
public IList<Item> Items { get; set; }
}
public class Item
{
public int Value { get; set; }
public string Text { get; set; }
public string Prefix { get; set; }
public string Label { get; set; }
}
Then change your code to this:
var result = new Result
{
Percentage = "hide",
Bullets = (string)null,
Items = {
new Item {
Value = 16,
Text = "Day",
Prefix = (string)null,
Label = (string)null
},
new Item {
Value = 41,
Text = "Week",
Prefix = (string)null,
Label = (string)null
},
new Item {
Value = 366,
Text = "Month",
Prefix = (string)null,
Label = (string)null
}
}
};
Addressed above with the structure.
Add to collection as follows:
result.Items.Add(new Item {
Value = 367,
Text = "Month",
Prefix = (string)null,
Label = (string)null
});
I would initialize the collection in the constructor as above.
To return the json from your controller action add the following:
return Json(result, JsonRequestBehavior.AllowGet);