0

Unable to find where I'm doing wrong. Help would be appreciated.Thanks!

In my viewmodel I have property ListValues which contain data for dropdownlist. I'm trying to set property(SelectedItem) on viewmodel when a selection is made in dropdownlist. But it is not updating the value when something is selected in dropdownlist. In this example I'm using javascript function to test if the value has been set, but it always says variable undefined.

My ViewModel:

public class ViewModel
    {
        private readonly List<SelectListItem> items;
        public ViewModel()
        {
            items = new List<SelectListItem>();
            items.Add(new SelectListItem() { Text = "Texas", Value = "1", Selected = true });
            items.Add(new SelectListItem() { Text = "Illinios", Value = "2", Selected = false });
            items.Add(new SelectListItem() { Text = "New York", Value = "3", Selected = false });
            items.Add(new SelectListItem() { Text = "Kansas", Value = "4", Selected = false });

            ListValues = items;
        }
        public string SelectedItem { get; set; }
        public IEnumerable<SelectListItem> ListValues { get; set; } 
    }

Action Method:

public class EmployersController : Controller
    {
        //
        // GET: /Employers/

        public ActionResult Index()
        {
            ViewModel vm = new ViewModel();
            return View(vm);
        }

    }

View:

@using System.Collections
@model DemoMVCApplication.Models.ViewModel
<script src="~/Scripts/Custom/default.js"></script>
@{
    ViewBag.Title = "Index";
}

@Html.DropDownListFor(model => model.SelectedItem, Model.ListValues)
<button id="button123"  onclick="process(@Model.SelectedItem);">Call Script</button>
<h2>Index</h2>

javascript to test if value has been set

function process(x) {
    var result = x;

}

The x always says undefined

kln
  • 1
  • 1

1 Answers1

0

this is a duplicate of: How to write a simple Html.DropDownListFor()?

however: <%= Html.DropDownListFor(n => n.MyColorId, new SelectList(Colors, "ColorId", "Name")) %>

is the code from that answer that you require, this being that you also need to convert your type to selectList, despite this already being that type in the Model.

Community
  • 1
  • 1
davethecoder
  • 3,856
  • 4
  • 35
  • 66
  • Looked at all options, but no luck. – kln Apr 04 '13 at 20:52
  • well @Model.SelectedItem would be nothing, because you are not setting a value for selected, what would be better is to look at the produced html and you will see that you function loks like this: process(); – davethecoder Apr 05 '13 at 08:02
  • This line should set the SelectedItem property. @Html.DropDownListFor(model => model.SelectedItem, Model.ListValues). Is something I'm doing wrong? – kln Apr 05 '13 at 14:53
  • yes, your code simply returns an empty / new viewmodel so there are no values unless you populate a model to begin with – davethecoder Apr 05 '13 at 14:57
  • The viewmodel has values to show, I have a constructor which does that. Only upon the selection in dropdown, the selected values is not set for SelectedItem. – kln Apr 05 '13 at 15:05
  • yeah I have just seen that you have a selected item in your list, but in the javascript call, you are using the separate property of public string SelectedItem { get; set; }, this will not have a value as you have not set a value, but simply returned the model, you need to set this property too and not just fill your list, either that or use javascript to get the selected item on change, rather than the property value you are trying to set – davethecoder Apr 05 '13 at 15:28