微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ASP.NET MVC:如何调用方法并在控制器中填充 SelectList?

如何解决ASP.NET MVC:如何调用方法并在控制器中填充 SelectList?

我需要帮助用我的模型中的值填充下拉列表。下面是我的代码。它不是 Razor Page 应用程序,所以我可能遗漏了一些东西。

控制器:

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;
            }
        }
    }
}

模型

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);

                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;
            }
        }
    }
}

自动生成的视图

@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>

最后是我的保管箱

@model MappingMVC.Models.Countries
@Html.DropDownList("drpdwnCountry",Model.CountryList,"-Select Country-")

有什么我想补充的吗?如何调用 GetCountry() 方法以启动 Model.CountryList

当我运行代码时,下拉列表标签在这文章标题中给出了 NullReference 错误

提前感谢您的帮助。

/詹姆斯

解决方法

试试这个:

修复操作代码:

var ctryCountries = new Countries();
ctryCountries.CountryList = new SelectList(ctryCountries.GetCountries(),"CountryID","CountryName");

并修复下拉列表:

@Html.DropDownListFor(m => m.CountryId,Model.CountryList,"-Select Country-",new { @class = "form-control" })

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。