I need help populating a dropdownlist with values from my model. Below is my code. It is not a Razor Page application so maybe I am missing something.
The controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MappingMVC.Models;
namespace MappingMVC.Controllers
{
public class CountryController : Controller
{
// GET: Country
[HttpGet]
public ActionResult Index()
{
try
{
Countries ctryCountries = new Countries();
ctryCountries.CountryList = new SelectList(ctryCountries.GetCountries(), "COUNTRYID", "COUNTRYNAME");
return View(ctryCountries);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
}
The model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.Mvc;
namespace MappingMVC.Models
{
public class Countries
{
private string strCountryCode;
private string strCountryName;
private string strCurrencyName;
private string strCurrencyCode;
private string strCountryTravelAgentCode;
private long lCountryID;
private string strCountrySovereign;
private string strCountryPhoneCode;
private DataSet dtCountryDataset;
private int iCountryAllowedCitizenship;
private SelectList lsCountryList;
#region "Properties"
public string CountryCode
{
//USED TO SET AND RETRIEVE THE COUNTRY CODE STRING VALUE
get { return strCountryCode; }
set { strCountryCode = value; }
}
public string CountryName
{
//USED TO SET AND RETRIEVE THE COUNTRY NAME STRING VALUE
get { return strCountryName; }
set { strCountryName = value; }
}
public string CurrencyName
{
//USED TO SET AND RETRIEVE THE CONNECTION STRING VALUE
get { return strCurrencyName; }
set { strCurrencyName = value; }
}
public string CurrencyCode
{
//USED TO SET AND RETRIEVE THE CURRENCY CODE STRING VALUE
get { return strCurrencyCode; }
set { strCurrencyCode = value; }
}
public string CountryTravelAgentCode
{
get { return strCountryTravelAgentCode; }
set { strCountryTravelAgentCode = value; }
}
public long CountryID
{
get { return lCountryID; }
set { lCountryID = value; }
}
public string CountryPhoneCode
{
get { return strCountryPhoneCode; }
set { strCountryPhoneCode = value; }
}
public string CountrySovereign
{
get { return strCountrySovereign; }
set { strCountrySovereign = value; }
}
public DataSet CountryDataset
{
//USED TO SET AND RETRIEVE THE COUNTRY CODE STRING VALUE
get { return dtCountryDataset; }
}
// Number of allowed citizenships for citizens of this country
public int CitizenshipNumberMaximum
{
get { return iCountryAllowedCitizenship; }
set { iCountryAllowedCitizenship = value; }
}
public DataSet DataSetCountries
{
get { return dtCountryDataset; }
set { dtCountryDataset = value; }
}
public SelectList CountryList
{
get { return lsCountryList; }
set { lsCountryList = value; }
}
#endregion
// return list of countries
public IEnumerable<Countries> GetCountries()
{
try
{
DataSet datCountries;
MappingLookupsReference.MappingLookupsSoapClient shLookup =
new MappingLookupsReference.MappingLookupsSoapClient();
datCountries = shLookup.ReturnCountryIDAndNamebyCountryName("", 0, 0, 0);
Countries objCountries = new Countries();
List<Countries> countryList = new List<Countries>();
countryList = datCountries.Tables[0].AsEnumerable()
.Select(dataRow =>
new Countries
{
CountryName = dataRow.Field<string>("COUNTRYNAME"),
CountryID = dataRow.Field<int>("COUNTRYID"),
CountryCode = dataRow.Field<string>("COUNTRYCODE"),
CountryPhoneCode = dataRow.Field<string>("COUNTRYPHONECODE"),
CurrencyName = dataRow.Field<string>("CURRENCYNAME"),
CurrencyCode = dataRow.Field<string>("CURRENCYCODE"),
CountrySovereign = dataRow.Field<string>("COUNTRYSOVEREIGN"),
strCountryTravelAgentCode = dataRow.Field<string>("COUNTRYTRAVELAGENTCODE"),
iCountryAllowedCitizenship = dataRow.Field<int>("COUNTRYTRAVELAGENTCODE")
}).ToList();
return countryList;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
}
The autogenerated view
@model MappingVC.Models.Countries
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
<h4>Countries</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.CountryCode)
</dt>
<dd>
@Html.DisplayFor(model => model.CountryCode)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CountryName)
</dt>
<dd>
@Html.DisplayFor(model => model.CountryName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CurrencyName)
</dt>
<dd>
@Html.DisplayFor(model => model.CurrencyName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CurrencyCode)
</dt>
<dd>
@Html.DisplayFor(model => model.CurrencyCode)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CountryTravelAgentCode)
</dt>
<dd>
@Html.DisplayFor(model => model.CountryTravelAgentCode)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CountryID)
</dt>
<dd>
@Html.DisplayFor(model => model.CountryID)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CountryPhoneCode)
</dt>
<dd>
@Html.DisplayFor(model => model.CountryPhoneCode)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CountrySovereign)
</dt>
<dd>
@Html.DisplayFor(model => model.CountrySovereign)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CitizenshipNumberMaximum)
</dt>
<dd>
@Html.DisplayFor(model => model.CitizenshipNumberMaximum)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
@Html.ActionLink("Back to List", "Index")
</p>
And finally my dropbox
@model MappingMVC.Models.Countries
@Html.DropDownList("drpdwnCountry", Model.CountryList, "-Select Country-")
Is there something that I am missing to add? How is the GetCountry()
method called so as to initiate the Model.CountryList
?
When I run the code, the dropdownlist tag gives me the NullReference error in the title of this post.
Thanks for the help in advance.
/James
question from:
https://stackoverflow.com/questions/65864748/asp-net-mvc-how-do-i-call-a-method-and-populate-a-selectlist-in-a-controller 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…