0

Im extremely new to .net core and trying to teach myself a few things just for the fun of it.

I am trying to pass a list of countries through to a form from a Country Model.

Club Model

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace SoccerExchange.Models
{
    public class Club
    {
        public int Id { get; set; }
        public string ClubName { get; set; }
        public string LeagueName { get; set; }
        public string Standard { get; set; }

        public ICollection<Advert> Adverts { get; set; }
    }
}

Country Model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace SoccerExchange.Models
{
    public class Country
    {
        public int Id { get; set; }
        public string CountryName { get; set; }

        public ICollection<Club> Clubs { get; set; }
    }
}

ViewModel

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Rendering;
using SoccerExchange.Models;

namespace SoccerExchange.ViewModels

{
    public class NewClubViewModel
    {
        public SelectList CountriesList { get; set; }
        public Club Club { get; set; }
    }
}

Create Function in Controller

public IActionResult Create()
        {
            var countries = _context.Countries.ToList();

            var viewModel = new NewClubViewModel
            {
                CountriesList = new SelectList(countries, "Id", "CountryName")
            };

            return View(viewModel);
        }

Create View

@model SoccerExchange.ViewModels.NewClubViewModel

@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Club</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Club.ClubName" class="control-label"></label>
                <input asp-for="Club.ClubName" class="form-control" />
                <span asp-validation-for="Club.ClubName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Club.LeagueName" class="control-label"></label>
                <input asp-for="Club.LeagueName" class="form-control" />
                <span asp-validation-for="Club.LeagueName" class="text-danger"></span>
            </div>

            <div class="form-group">
                <label asp-for="Club.Standard" class="control-label"></label>
                <input asp-for="Club.Standard" class="form-control" />
                <span asp-validation-for="Club.Standard" class="text-danger"></span>
            </div>
            <div>
                @Html.DropDownListFor(m => Model.CountriesList, "--Please Select--")
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

I am almost certain that I have the Models along with the ViewModels correct. Im also almost certain that I have the create method correct. But for some reason, I can not pass through the list data for the drop down in the form.

Would anyone have any hints on what I Might be doing wrong?

2 Answers2

0

Html.DropDownListFor expects the data source as the second parameter (see below).

Image of Intellisense for DropDownListFor

This article may be some help for you ASP.NET MVC Dropdown List From SelectList

0

You need to fix your clab class by adding Country

 public class Club
    {
        public int Id { get; set; }
        public string ClubName { get; set; }

        public int CountryId { get; set; }
        public virtual Country Country { get; set; }

        .....
    }

and fix the view

 @Html.DropDownListFor(m =>m.Club.CountryId,  @Model.CountriesList, "--Please Select--")
Serge
  • 40,935
  • 4
  • 18
  • 45