【问题标题】:MVC razor view dropdownbox with items from modelMVC razor 视图下拉框,其中包含模型中的项目
【发布时间】:2015-04-16 11:07:57
【问题描述】:

我是 MVC.Net 和 razorviews 的新手。 我有一个包含卡片列表的模型。我想在视图内的下拉框中显示此列表。但我不被允许。从一个例子中,我找到了这段代码并进行了尝试:

构成列表的类:

public class CardType
{
    public CardType(string cardTypeName)
    {
        this.CardTypeName = cardTypeName;
    }

    public string CardTypeName { get; set; }
}

型号:

public class MyCardsViewModel
{
    public IEnumerable<PaymentCard> PaymentCards { get; set; }
    public AddPaymentcard PaymentCardInformation { get; set; }
}

public class AddPaymentcard
{
   public string CardTypeName { get; set; }

    public IEnumerable<SelectListItem> CardTypeList
    {
        get
        {
        return _cardTypes.Select(x => new SelectListItem {Value = x.CardTypeName, Text = x.CardTypeName}).ToList(); 
        }
    }

    public List<CardType> _cardTypes;
 }

控制器方法:

    // GET: /MyCards/
    public ActionResult Index()
    {
        string userId = User.Identity.GetUserId();
        MyCardsViewModel viewModel = new MyCardsViewModel();
        var user = _userRepository.GetUser(userId);

        viewModel.PaymentCards = user.PaymentCards; //Don't mind this, it is something else :)
        var cardTypeList = _cardTypeRepository.GetCardTypes();

        viewModel.PaymentCardInformation=new AddPaymentcard 
        {
           _cardTypes = cardTypeList
        };

        return View(viewModel);
    }

 //Get: MyCards/AddPaymentcard
    public ActionResult AddPaymentcard()
    {
        return View();
    }

观看次数:

接收 MyCardsViewModel 并添加部分视图 AddPaymentcard.cshtml 的索引视图:

 @model Kvittering.TaxiFinans.Web.Models.MyCardsViewModel
 (...)
    @Html.Partial("~/Views/MyCards/AddPaymentcard.cshtml", @Model.PaymentCardInformation );

添加支付卡视图:

 @model Kvittering.TaxiFinans.Web.Models.AddPaymentcard

 (...)
 <div class="form-group">
    @Html.LabelFor(m => m.CardTypeName, new { @class = "control-label col-md-2"})
    <div class="col-md-offset-2 col-md-8">
        @Html.DropDownListFor(m => m.CardTypeName, @Model.CardTypeList)
    </div>
  </div>

谁能帮我解决这个问题? :/

【问题讨论】:

  • 它必须是 get { return new SelectList(_cardTypes, "CardTypeName", "CardTypeName"); } - 您需要同时指定 ValueText 属性
  • 你也可以签名public SelectList CardTypeList {..},但无论如何属性应该是public SelectList CardTypeList { get; set; }并且控制器应该负责设置值(List&lt;CardType&gt; _cardTypes;不是必需的)

标签: c# asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

如果您 Selectnew SelectListItem 可以解决一个问题,这样您就可以停止使用硬编码字符串,并且您将确保获得您想要的。

public class AddPaymentcard
{
   public string CardTypeName { get; set; }

    public IEnumerable<SelectListItem> CardTypeList
    {
        get { return _cardTypes.Select(x => new SelectListItem { Value = x.CardTypeName, Text = x.CardTypeName }); }
    }

    public List<CardType> _cardTypes;
 }

如果您的视图模型类型为MyCardsViewModel,则必须更正此行

@Html.DropDownListFor(m => m.PaymentCardInformation.CardTypeName, @Model.PaymentCardInformation.CardTypeList)

【讨论】:

  • 其实没必要,属性为IEnumerable时加.ToList()有点无意义
  • @StephenMuecke 你说得对.ToList() 我是出于习惯。我认为这比使用SelectList 更好,因为你没有硬编码字符串,而且你确定你获得了你想要的SelectListItem 集合。
  • 谢谢,这似乎消除了构建错误。但是在运行时,我在“@Html.DropDownListFor(m => m.CardTypeName, Model.CardTypeList)”行的视图中得到一个空指针,并带有错误消息:App_Web_qzig3nv5.dll 中发生了“System.NullReferenceException”类型的异常,但是未在用户代码中处理附加信息:对象引用未设置为对象的实例。
  • 无论哪种方式都可以,但 OP 的问题是他使用了错误的构造函数
  • @adricadar,我怀疑它也需要是 m =&gt; m.PaymentCardInformation.CardTypeName
猜你喜欢
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
  • 2011-06-10
  • 1970-01-01
  • 2011-05-26
  • 1970-01-01
  • 2017-05-04
相关资源
最近更新 更多