Home ASP.NET MVC: View does not display updated model
Reply: 0

ASP.NET MVC: View does not display updated model

user1906 Published in June 20, 2018, 1:22 pm

I can't get my head around this one. I have a Html.DropDownList, I simply want to display the value of the selected item in a Html.Display item.


public class MyModel
    private List<string> _items = null;
    private string _selectedItems = string.Empty;

    public MyModel()
        _items = new List<string> { "item1", "item2", "item3" };
        _selectedItems = _items[0];

    public List<string> Items { get { return _items; } }

    public string SelectedItem
        get{ return _selectedItems; }
        set { if (_selectedItems != value) { _selectedItems = value; } }


@model MyModel

@Html.DropDownListFor(model => model.SelectedItem, new 
SelectList(@Model.Items), new { onchange = "SelectedItemChanged(this);" })
@Html.DisplayFor(model => model.SelectedItem)

Controller's action

    public ActionResult OnSelectedItemChanged(string itemName)
        return View("Index", new MyModel { SelectedItem = itemName }); 

I use ajax to call the controller's method - in my View, in the script section I have:

function SelectedItemChanged(selectObj) {
    itemName = selectObj.value;
        type: 'POST',
        url: '@Url.Action("OnSelectedItemChanged","Home")',
        data: { 'itemName': itemName },

The controller gets called properly but for some reason the view displayed by the browser does not get updated, and the text field still displays the initial "item1" value. How come?

I should also note that, looking in the "response" tab of the ajax request, the response html is updated correctly!

What am I doing wrong?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.512358 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO