Setting selected value on DropDownListFor - c#

I'm attempting to set the selected value of a combobox depending on what the controller has set.
My DropDownListFor:
#if (Model.FormType == "Detailed")
{
#Html.DropDownListFor(model => model.FormType,
new SelectList(Enum.GetValues(typeof(FormType))),
"Detailed")
}
else
{
#Html.DropDownListFor(model => model.FormType,
new SelectList(Enum.GetValues(typeof(FormType))),
"Summary")
}
My FormType enum:
public enum FormType
{
Summary,
Detailed
}
When I try this, it's duplicating "Summary" again and selecting it so I end up with duplicate entries:
What am I doing wrong? Thank you!

Your "Detailed" or "Summary" just a title of dropdown list. You can change it to "Select" for example. But if you need to show a selected item :
You need an util to create a SelectList
public static SelectList GetSelectList(string selectedText)
{
Array values = Enum.GetValues(typeof(FormType));
List<SelectListItem> items = new List<SelectListItem>();
foreach (var i in values)
{
var item=new SelectListItem
{
Text = Enum.GetName(typeof(FormType), i),
Value = i. ToString()
};
if(item.Text==selectedText) item.Selected=true;
items.Add(item);
}
return new SelectList(items);
}
Add SelectList to your view model:
public Model ()
{
..... your code
public SelectList SelectList {get; set;}
{
after this you can put this code in your action
model.SelectList=GetSelectList(model.FormType);
and finaly the view:
#Html.DropDownListFor(model => model.FormType, model.SelectList,"Select")

Related

return model to view on error with selectlistitem default value

i have some issues with default value of my dropdownlist when returning my model to view in case of one or many errors. I have a dropdownlist in the view which is filled from the controller and others empty dropdownlists in the same view which are filled with JSON on selection of the first dropdownlist.
public ActionResult Countriesdata()
{
CountrydetailsViewModel vm= new CountrydetailsViewModel();
vm.countries= dal.countries().Select(x => new SelectListItem { Text = x.Name, Value = x.CountryID.ToString() })
.ToList();
return View(vm);
}
here, dal is my data access layer and allows me to fill the list of countries from the database. The code use to fill the countries list in the view is like this
#Html.DropDownListFor(m => m.selectedcountry, new SelectList(Model.countries, "Value", "Text", Model.selectedcountry), "-Select a Country-", new { #class = "ddlist" })
one of the empty dropdowlists is as the one below
#Html.DropDownListFor(m => m.selectedtown, new SelectList(Enumerable.Empty<SelectListItem>(), "Value", "Text", Model.selectedtown), "-Select a Town/City-", new { #class = "ddlist" })
This code work very well i reach the page for the first time because i have set a default value for country dropdownlist which is select a country. i use the following code to post my form.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Countriesdata(CountrydetailsViewModel returnmodel)
{
if (! ModelState.IsValid)
{
returnmodel.countries= dal.countries().Select(x => new SelectListItem { Text = x.Name, Value = x.CountryID.ToString() })
.ToList();
return View(returnmodel);
}
return RedirectToAction("mainpage");
}
If the form contains errors, my model is returned back to my view with the posted value of country selected dropdownlist as default, which is not my goal because the others dropdowlists which are filled using JSON on the country dropdownlist selection change are empty.Thus, I ought to select this same country once to fill the others dropdowlists, which is cumbersome. To be logic, i would like to send back my model to my view with default value of the dropdowlist of country when an error occurs. I am using MVC4 and VS 2010
You need to populate both SelectList's in the controller methods so they get passed to the view. In the GET method, the 2nd one will be an empty SelectList (assuming its a 'Create' metod), but in the POST method it will be populated based on the country that has been selected.
You model should include
public class CountrydetailsViewModel
{
[Required(Error Message = "..")]
public int? SelectedCountry { get; set; }
[Required(Error Message = "..")]
public int? SelectedTown { get; set; }
....
public IEnumerable<SelectListItem> CountryList{ get; set; }
public IEnumerable<SelectListItem> TownList { get; set; }
}
And your controller methods
public ActionResult Countriesdata()
{
CountrydetailsViewModel vm = new CountrydetailsViewModel();
ConfigureViewModel(vm);
return View(vm);
}
[HttpPost]
public ActionResult Countriesdata(CountrydetailsViewModel returnmodel)
{
if(!ModelState.IsValid)
{
ConfigureViewModel(returnmodel);
return View(returnmodel);
}
.... // save and redirect
}
private ConfigureViewModel(CountrydetailsViewModel model)
{
var countries = dal.countries();
model.CountryList= countries.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.CountryID.ToString()
});
if (model.SelectedCountry.HasValue)
{
// adjust query to suit your property names
var towns = db.towns.Where(e => e.CountryId == model.SelectedCountry);
model.TownList = towns.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.TownID.ToString()
});
}
else
{
model.TownList = new SelectList(Enumerable.Empty<SelectListItem>());
}
}
This also allows you to generate the correct options and default selections when editing an existing CountrydetailsViewModel.
Then in the view, use
#Html.DropDownListFor(m => m.SelectedCountry, Model.CountryList, "-Select a Country-", new { #class = "ddlist" })
#Html.ValidationMessageFor(m => m.SelectedCountry)
#Html.DropDownListFor(m => m.SelectedTown, Model.TownList, "-Select a Country-", new { #class = "ddlist" })
#Html.ValidationMessageFor(m => m.SelectedTown)
Note that there is no point creating an identical SelectList from the original one you passed to the view by using new SelectList(..) - its just unnecessary extra overhead. Note also that the last parameter in the SelectList constructor is ignored when your binding to a model property (internally the method builds its own SelectList based on the value of the property) - you could put whatever value you wanted as the last parameter and you will see that the option is still correct selected based on the value of the property.

How can I capture the #Html.DropDownListFor selected value?

I am using MVC5, Razor, Entity Framework, C#. I am trying to pass a value of a dorpdown list using a link.
my model is
public class TestVM
{
public string TheID { get; set; }
}
I am loading an enum into a IEnumerable<SelectListItem>.
My enum is
public enum DiscountENUM
{
SaleCustomer,
SaleCustomerCategory,
SaleProduct,
SaleProductCategory,
SaleCustomerAndProduct,
SaleCustomerAndProductCategory,
SaleCustomerCategoryAndProductCategory,
PurchaseVendor,
PurchaseVendorAndProduct,
PurchaseVendorAndProductCategory,
PurchaseProduct,
PurchaseProductCategory,
Unknown
}
I am using the index method of the home controller
public ActionResult Index()
{
ViewBag.ListOfDiscounts = SelectListDiscountENUM();
TestVM d = new TestVM();
return View(d);
}
Where I load the ListOfDiscounts using:
private IEnumerable<SelectListItem> SelectListDiscountENUM()
{
List<SelectListItem> selectList = new List<SelectListItem>();
var listOfEnumValues = Enum.GetValues(typeof(DiscountENUM));
if (listOfEnumValues != null)
if (listOfEnumValues.Length > 0)
{
foreach (var item in listOfEnumValues)
{
SelectListItem sVM = new SelectListItem();
sVM.Value = item.ToString();
sVM.Text = Enum.GetName(typeof(DiscountENUM), item).ToString();
selectList.Add(sVM);
}
}
return selectList.OrderBy(x => x.Text).AsEnumerable();
}
My create method which is called from the view is
public ActionResult Create(TestVM d, string TheID)
{
return View();
}
My Index view is
#model ModelsClassLibrary.Models.DiscountNS.TestVM
<div>#Html.ActionLink("Create New", "Create", new { TheID = Model.TheID})</div>
<div>
#Html.DropDownListFor(x => x.TheID, #ViewBag.ListOfDiscounts as IEnumerable<SelectListItem>, "--- Select Discount Type ---", new { #class = "form-control" })
</div>
The problem is in the following line in the View
<div>#Html.ActionLink("Create New", "Create", new { TheID = Model.TheID })</div>
I have tried adding a model with the name of the field as "TheID"... no luck. Also, added a string field in the parameter, no luck. I looked at the FormControl object, and there was nothing in it either! I suspect something has to be added at the Route level in the helper, but I don't know what.
Model.TheID is always null. Even when I select an item in the DropDownListFor.
Does anyone have an idea how I can capture the select value of the DropDownListFor and send it into the Html.ActionLink TheID?

MVC DropDownList For Selected Value

I am trying to populate the selected value field of an MVC drop down list, but I have been unsuccessful. I am using MVC 5.1.
Here is a screenshot of the selected value and the model showing sales receipts:
Instead of sales receipts being selected, the model defaults to estimates:
When I use ViewData, the drop down works successfully:
Html.DropDownList("JMASettings.InvoiceMode")
Here is my code:
public void GetInvoiceMode(JMASettings model)
{
Dictionary<string, string> modes = new Dictionary<string, string>();
modes.Add("Estimates", "Estimates");
modes.Add("Invoices Only (Payments If Paid)", "Invoices");
modes.Add("Invoices No Payments", "InvoicesNoPayments");
modes.Add("Sales Receipts", "Sales Receipts");
if (Session["dataSource"] != null && Session["dataSource"].ToString() == "QBD")
modes.Add("Sales Orders", "Sales Orders");
IList<SelectListItem> ilSelectList = new List<SelectListItem>();
foreach (KeyValuePair<string, string> mode in modes)
{
SelectListItem selectListItem = new SelectListItem();
selectListItem.Text = mode.Key;
selectListItem.Value = mode.Value;
if (model.InvoiceMode == mode.Value)
selectListItem.Selected = true;
else if (String.IsNullOrEmpty(model.InvoiceMode) && mode.Key == "Sales Receipts")
selectListItem.Selected = true;
ilSelectList.Add(selectListItem);
}
ViewData["JMASettings.InvoiceMode"] = ilSelectList;
}
Keep the property which represents the selected item on your view model like this.
public class CreateOrderVM
{
public List<SelectListItem> Modes {set;get;}
public string SelectedMode {set;get;}
public CreateOrderVM()
{
this.Modes=new List<SelectListItem>();
}
}
Now when you have to render the view, load the Modes collection and set the SelectedMode to whatever you want
public ActionResult Show()
{
var vm=new CreateOrderVM();
vm.Modes = GetModes();
//Set one of them as selected
vm.SelectedMode="B";
return View(vm);
}
private List<SelectListItem> GetModes()
{
var list=new List<SelectListItem>();
//hard coded for demo.Replace with actual values
list.Add(new SelectListItem { Value="A", Text="A"});
list.Add(new SelectListItem { Value="B", Text="B"});
list.Add(new SelectListItem { Value="C", Text="C"});
return list;
}
In your view,
#model YourNamespace.CreateOrderVM
#Html.DropdownListFor(s=>s.SelectedMode,Model.Modes,"Select")

Set selected index of dropdown to zero after form submit in ASP.NET MVC

I am bit to new asp.net mvc and using aps.net mvc 5. I have create the below dropdown using html helpers in aps.net mvc. When i submit(post back) the form i want to set the selected index to zero. Here i am using a optionLabel "--select--". I want to set the selected value to that one ("--select--") after post back. How to achieve this. Please help. Thank you.
#Html.DropDownListFor(model => model.TestCategory, new SelectList(#ViewBag.TestCategories, "value", "text"), "-- Select --", new { #class = "form-control input-sm"})
Controller Code
[HttpGet]
public ActionResult Index()
{
var model = new LaboratoryViewModel {
medicaltestlist = new List<MedicalTest>()
};
PopTestCategory();
PopEmptyDropdown();
return View(model);
}
[HttpPost]
public ActionResult Index(LaboratoryViewModel labvm)
{
var test = PopMedicalTests().Where(x => x.TestSerial == Convert.ToInt32(labvm.TestCode)).FirstOrDefault();
if (labvm.medicaltestlist == null)
labvm.medicaltestlist = new List<MedicalTest>();
if(!labvm.medicaltestlist.Any(x=> x.TestSerial == test.TestSerial))
labvm.medicaltestlist.Add(test);
labvm.TestCategory = "";
PopTestCategory();
return View(labvm);
}
public void PopTestCategory()
{
var categorylist = new List<DropDownItem>
{
new DropDownItem{value="Medical",text="Medical"},
new DropDownItem{value="Animal",text="Animal"},
new DropDownItem{value="Food",text="Food"},
new DropDownItem{value="Water",text="Water"}
};
ViewBag.TestCategories = categorylist;
}
public class DropDownItem
{
public int id { get; set; }
public string value { get; set; }
public string text { get; set; }
}
You return the view in you post method so if you selected (say) Animal then that value will be selected when you return the view because the html helpers use the values from ModelState, not the model property. Setting labvm.TestCategory = ""; has no effect. The correct approach is to follow the PRG pattern and redirect to the GET method, however you can make this work by calling ModelState.Clear(); before setting resetting the value of TestCategory although this will clear all ModelState properties and errors and may have other side effects.
Side note: You DropDownItem class seems unnecessary. MVC already has a SelectListItem class designed to work with dropdownlists, and in any case you can replace all the code in your PopEmptyDropdown() method with
ViewBag.TestCategories = new SelectList(new List<string>() { "Medical", "Animal", "Food", "Water" });
and in the view
#Html.DropDownListFor(m => m.TestCategory, (SelectList)#ViewBag.TestCategories, "-- Select --", new { #class = "form-control input-sm"})
If you set the "value" attribute of the top item in the drop down list to something and then pass back a model containing that for the bound property it should work?

Convert Dropdownlist item's value to int

I need the value of SelectListItem to be int.
So I pull it of the database, convert it to string in the process and store to listitem's value.
public class BookAdd
{
public BookAdd()
{
public Book Book { get; set; }
DataModelContainer db = new DataModelContainer();
IEnumerable<SelectListItem> items = db.PublisherSet
.Select(i => new SelectListItem
{
Value = SqlFunctions.StringConvert((double)i.Id),
Text = i.Name
});
}
}
I then need to store the value again as int to Book.PublisherId when selected from dropdownlist. I know the code below is not complete, I figured I need somehow convert the selected item's value to int, how do I do it?
#model Bookshops.Models.BookAdd
...
#Html.DropDownListFor(model => model.Book.PublisherId, Model.items);
And finaly controler:
public ActionResult Create2()
{
var bookAdd = new BookAdd();
ViewBag.Publisher = bookAdd.items;
return View(bookAdd);
}
//
// POST: /Book/Create
[HttpPost]
public ActionResult Create2(BookAdd book)
{
if (ModelState.IsValid)
{
Book book2 = new Book();
book2.Id = book.Book.Id;
book2.AuthorId = book.Book.AuthorId;
book2.Isbn = book.Book.Isbn;
book2.Id = int.Parse(book.Book.PubId);
book2.Title = book.Book.Title;
book2.YearPublished = book.Book.YearPublished;
db.BookSet.Add(book2);
db.SaveChanges();
}
return RedirectToAction("Index");
}
I'm using MVC 3, EF 4.
Instead of building a SelectListItem, I built something like this:
public struct BookItem
{
int id;
string name;
}
Then add the following item to the model and select data into it
IEnumerable<BookItem> bookList {get; set;}
Fianally
#Html.DropDownListFor(model => model.Book.PublicherId, new SelectList(Model.bookList, "id", "name"))
When the selected Item of the DropDownList will get posted back convert it to int from string like this Convert.ToInt32(DropDownSelectedItem) or with Int32.TryParse(value, out number)
to start you can simplify the mapping code to
db
.PublisherSet
.Select(x=> new SelectListItem{Value = x.Id.ToString(), Text = x.Name})
.ToArray();
In the view you can then do
#Html.DropDownListFor(model => model.Book.PublisherId, Model.items);
and it will properly render a select element with the options and the appropriate publisher selected.
You can then use the model binding conventions to pass this back to the controller action when the user clicks the submit button.
In the end, it's all text. that's the nature of the web. we have a lot of tricks, converters and binders to turn string dictionaries into rich view models, but ultimately it's just strings.

Categories

Resources