Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7559455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:38:35+00:00 2026-05-30T12:38:35+00:00

I come from a more WPF application background and I’m used to bindings and

  • 0

I come from a more WPF application background and I’m used to bindings and such. Jumping into websites then can come with it’s problems as they work so much more differently.

I’m trying to do a simple Ajax action but not sure where to begin. Basically I want to make a dropdownlist that changes one property on the model and re-renders that part of the page. Perhaps this is too much of a WPF way to do this so my Model could be twisted for the thing it is supposed to do.

Here is what I got already:

public class TheViewModel
{
    private IEnumerable<TheData> _data;

    public TheViewModel(IEnumerable<TheData> data)
    {
        _data = data;
        Year = 2012;
    }

    public int Year { get; set; }

    public ICollection<TheData> Data
    {
        get
        {
            return _data.Where(d => d.Year == this.Year).ToList();
        }
    }

     public IEnumerable<SelectListItem> YearList
     {
        // lists the available years
     }
}

public class TheData
{
    public int Year { get; set; }
    //Some more info I want to represent in Table
}

And the razor:

@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "thetable" }))
{
        @Html.DropDownListFor(model => model.Year, Model.YearList, new { AutoPostBack = "true"})
        <input type="submit" name="name" value="Submit" />
}


<table id="thetable">
    <thead>
        //some headers
    </thead>
    <tbody>
    @foreach ( var item in Model.Data)
    {
        //row for each data
    }
    </tbody>
</table>

As you can see I’m hoping for the Year property to be updated and a new call to be made for the Data property which would result in new information. That information would be rendered in thetable Table

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:38:36+00:00Added an answer on May 30, 2026 at 12:38 pm

    Here’s a full example.

    Model:

    public class MyViewModel
    {
        public int Year { get; set; }
        public IEnumerable<SelectListItem> Years
        {
            get
            {
                return Enumerable.Range(1980, 40).Select(x => new SelectListItem
                {
                    Value = x.ToString(),
                    Text = x.ToString()
                });
            }
        }
    
        public IList<TheData> Data { get; set; }
    }
    
    public class TheData
    {
        public int Year { get; set; }
        public string Foo { get; set; }
        public string Bar { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel();
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(int year)
        {
            var model = new[]
            {
                new TheData { Year = year, Foo = "foo 1", Bar = "bar 1" },
                new TheData { Year = year, Foo = "foo 2", Bar = "bar 2" },
                new TheData { Year = year, Foo = "foo 3", Bar = "bar 3" },
            };
            return PartialView("_data", model);
        }
    }
    

    Index.cshtml view:

    @model MyViewModel
    
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('#yearsddl').change(function () {
                $(this).closest('form').trigger('submit');
            });
        });
    </script>
    
    @using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "data" }))
    {
        @Html.DropDownListFor(x => x.Year, Model.Years, new { id = "yearsddl" })
    }
    
    <table>
        <thead>
            <tr>
                <th>Year</th>
                <th>Foo</th>
                <th>Bar</th>
            </tr>
        </thead>
        <tbody id="data">
            @Html.Partial("_data", Model.Data ?? Enumerable.Empty<TheData>())
        </tbody>
    </table>
    

    The jquery.unobtrusive-ajax.js script inclusion should be moved out of the index view inside the layout for example and the custom js that subscribes for the change event of the dropdownlist should be moved into a separate js file and included from the Layout. I just put them here to illustrate a full example of what’s required for the view to work.

    _Data.cshtml partial:

    @model IList<TheData>
    
    @for (int i = 0; i < Model.Count; i++)
    {
        <tr>
            <td>@Html.DisplayFor(x => x[i].Year)</td>
            <td>@Html.DisplayFor(x => x[i].Foo)</td>
            <td>@Html.DisplayFor(x => x[i].Bar)</td>
        </tr>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I come from more of a Java background. In the last year or two,
I come from a Java background, where packages are used, not namespaces. I'm used
I come from more of a C# background yet am learning Ruby in my
I come from a Java background. I am wanting to learn more about concurrency
I come from a C# background and need to become more familiar with JS.
I come from a mostly n-tier background, and I'm trying to move more towards
I come from a background in static languages. Can someone explain (ideally through example)
I come from more of an Android development background so apologies if this is
I come from more of a Python and CTYPES background...and am trying to work
I come from a mainly PHP background and make good use of the Apache

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.