ASP.NET CORE MVC select tag helper - how to set selected value without using ModelView
<p>In the following code of an ASP.NET MVC Core project, the Get action method <code>Test(...)</code> is displaying a dropdown list of years. By default, the dropdown displays the first year in the list (i.e. 2000) as a <code>Selected value</code>. If I want to display a particular year in the same list as a selected value, how can I achieve it <code>from action method</code> <code>Test(...)</code> <strong>without</strong> using ViewModel techinque? A solution for either a select tag helper or a DropdownList html helper will be fine - but Selected value has to set from action method.</p>

<p><strong>In short</strong>, how would you set a particular year as a Selected Value in this list: <code>ViewBag.YearsList = Enumerable.Range(2000, 15).Select(g =&gt; new SelectListItem { Value = g.ToString(), Text = g.ToString() }).ToList();</code></p>

<p><strong>Models</strong>:</p>

<pre><code>    public class BloggingContext : DbContext
    {
        public BloggingContext(DbContextOptions&lt;BloggingContext&gt; options)
            : base(options)
        { }

        public DbSet&lt;Blog&gt; Blogs { get; set; }
        public DbSet&lt;Post&gt; Posts { get; set; }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public IList&lt;Post&gt; Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public int PostYear { get; set; }
        public int BlogId { get; set; }
        public Blog Blog { get; set; }
}
</code></pre>

<p><strong>Controller</strong>: Only relevant action methods are shown.</p>

<pre><code> public class BlogsController : Controller
{
    private readonly BloggingContext _context;

    public BlogsController(BloggingContext context)
    {
        _context = context;    
    }

    // GET: Blogs
    public async Task&lt;IActionResult&gt; Index()
    {
        return View(_context.Blogs.ToList());
    }

    // GET: /Blogs/Test
    [HttpGet]
    public async Task&lt;IActionResult&gt; Test(string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
        ViewBag.YearsList = Enumerable.Range(2000, 15).Select(g =&gt; new SelectListItem { Value = g.ToString(), Text = g.ToString() }).ToList();

        //return View(await _context.Blogs.Include(p =&gt; p.Posts).ToListAsync());
        var qrVM = from b in _context.Blogs
                    join p in _context.Posts on b.BlogId equals p.BlogId into bp
                    from c in bp.DefaultIfEmpty()
                    select new BlogsWithRelatedPostsViewModel { BlogID = b.BlogId, PostID = (c == null ? 0 : c.PostId), Url = b.Url, Title = (c == null ? string.Empty : c.Title), Content = (c == null ? string.Empty : c.Content) };

        return View(await qrVM.ToListAsync());
    }

    // POST: /Blogs/Test
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task&lt;IActionResult&gt; Test(IList&lt;BlogsWithRelatedPostsViewModel&gt; model, string GO, int currentlySelectedIndex, string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
        ViewBag.YearsList = Enumerable.Range(2000, 15).Select(g =&gt; new SelectListItem { Value = g.ToString(), Text = g.ToString() }).ToList();

        if (!string.IsNullOrEmpty(GO))
        {
            var qrVM = from b in _context.Blogs
                        join p in _context.Posts on b.BlogId equals p.BlogId into bp
                        from c in bp.DefaultIfEmpty()
                        where c == null? true : c.PostYear.Equals(currentlySelectedIndex)
                        select new BlogsWithRelatedPostsViewModel { BlogID = b.BlogId, PostID = (c == null ? 0 : c.PostId), Url = b.Url, Title = (c == null ? string.Empty : c.Title), Content = (c == null ? string.Empty : c.Content) };
            return View(await qrVM.ToListAsync());
        }
        else if (ModelState.IsValid)
        {
            foreach (var item in model)
            {
                var oPost = _context.Posts.Where(r =&gt; r.PostId.Equals(item.PostID)).FirstOrDefault();
                if (oPost != null)
                {
                    oPost.Title = item.Title;
                    oPost.Content = item.Content;
                    oPost.PostYear = currentlySelectedIndex;
                    oPost.BlogId = item.BlogID; //according to new post below the blogId should exist for a newly created port - but just in case
                }
                else
                {
                    if (item.PostID == 0)
                    {
                        Post oPostNew = new Post { BlogId = item.BlogID, Title = item.Title, Content = item.Content, PostYear = currentlySelectedIndex }; //need to use currentlySelectedIndex intead of item.FiscalYear in case of adding a record
                        _context.Add(oPostNew);
                    }

                }
            }
            await _context.SaveChangesAsync();
            //return RedirectToLocal(returnUrl);
            return View(model);
        }

        // If we got this far, something failed, redisplay form
        return View();
    }
 }
</code></pre>

<p><strong>Test.cshtml View</strong></p>

<pre><code>@model IList&lt;ASP_Core_Blogs.Models.BlogPostViewModels.BlogsWithRelatedPostsViewModel&gt;

&lt;div class="row"&gt;
    &lt;div class="col-md-12"&gt;
        &lt;form asp-controller="Blogs" asp-action="Test" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post"&gt;
            @{
                IEnumerable&lt;SelectListItem&gt; yearsList = (IEnumerable&lt;SelectListItem&gt;)ViewBag.YearsList;
                var currentlySelectedIndex = 0; // Currently selected index (usually will come from model)
            }
            &lt;strong&gt;Select a Post Year&lt;/strong&gt;
            &lt;h6&gt;Choose a year and a URL to begin:&lt;/h6&gt;
            &lt;label&gt;Year:&lt;/label&gt;&lt;select asp-for="@currentlySelectedIndex" asp-items="yearsList"&gt;&lt;/select&gt;&lt;input type="submit" class="btn btn-default" name="GO" value="GO" /&gt;
            &lt;table class="table"&gt;
                &lt;thead&gt;
                    &lt;tr&gt;
                        &lt;th&gt;&lt;/th&gt;
                        &lt;th&gt;&lt;/th&gt;
                        &lt;th&gt;Url&lt;/th&gt;
                        &lt;th&gt;Title&lt;/th&gt;
                        &lt;th&gt;Content&lt;/th&gt;
                    &lt;/tr&gt;
                &lt;/thead&gt;
                &lt;tbody&gt;
                    @for (int i=0; i&lt; Model.Count(); i++)
                    {
                        &lt;tr&gt;
                            &lt;td&gt;@Html.HiddenFor(r =&gt; r[i].BlogID)&lt;/td&gt;
                            &lt;td&gt;@Html.HiddenFor(r =&gt; r[i].PostID)&lt;/td&gt;
                            &lt;td&gt;
                                @Html.TextBoxFor(r =&gt; r[i].Url)
                            &lt;/td&gt;
                            &lt;td&gt;
                                @Html.TextBoxFor(r =&gt; r[i].Title)
                            &lt;/td&gt;
                            &lt;td&gt;
                                @Html.TextBoxFor(r =&gt; r[i].Content)
                            &lt;/td&gt;
                        &lt;/tr&gt;
                    }
                &lt;/tbody&gt;
            &lt;/table&gt;
            &lt;button type="submit" class="btn btn-default"&gt;Save&lt;/button&gt;
        &lt;/form&gt;
    &lt;/div&gt;
&lt;/div&gt;
</code></pre>