【问题标题】:ASP.NET MVC 4 DropDownList SelectedValue doesn't workASP.NET MVC 4 DropDownList SelectedValue 不起作用
【发布时间】:2012-05-23 00:11:34
【问题描述】:

我正在尝试使用 DropDownList 帮助器在 ASP.NET MVC 4 应用程序中构建一个带有 selectedvalue 的选择列表,但是当生成下拉列表时,它没有任何选择的值,即使给定的 SelectList作为源设置了 SelectedValue。

代码如下:

我的模特:

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

namespace MvcApplication3.Models
{
    public class Conta
    {
        public long ContaId { get; set; }

        public string Nome { get; set; }

        public DateTime DataInicial { get; set; }

        public decimal SaldoInicial { get; set; }

        public string Owner;

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            if (obj.GetType() != typeof(Conta))
                return false;

            Conta conta = (Conta)obj;

            if ((this.ContaId == conta.ContaId) && (this.Owner.Equals(conta.Owner)) && (this.Nome.Equals(conta.Nome)))
                return true;

            return false;
        }

        public override int GetHashCode()
        {
            int hash = 13;

            hash = (hash * 7) + ContaId.GetHashCode();

            return hash;
        }
    }
}

我的控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication3.Models;

namespace MvcApplication3.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult View1()
        {
            Conta selecionada = new Conta()
            {
                ContaId = 3,
                Nome = "Ourocard VISA",
                Owner = "teste"
            };

            SelectList selectList = new SelectList(Contas(), "ContaId", "Nome", selecionada);

            ViewBag.ListaContas = selectList;

            return View();
        }

        IEnumerable<Conta> Contas()
        {
            yield return new Conta()
            {
                ContaId = 1,
                Nome = "Banco do Brasil",
                Owner = "teste"
            };

            yield return new Conta()
            {
                ContaId = 2,
                Nome = "Caixa Econômica",
                Owner = "teste"
            };

            yield return new Conta()
            {
            ContaId = 3,
                Nome = "Ourocard VISA",
                Owner = "teste"
            };

            yield return new Conta()
            {
                ContaId = 4,
                Nome = "American Express",
                Owner = "teste"
            };
        }
    }
}

我的观点:

<h2>View1</h2>

@Html.DropDownList("teste", ViewBag.ListaContas as SelectList)

下拉菜单是使用 Contas() 方法创建的四个选项构建的,但没有一个被选中。会是什么?

【问题讨论】:

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


    【解决方案1】:

    您应该将3 作为最后一个参数传递给 SelectList 构造函数,而不是一个对象。

    另外,您的 GetHashCode 函数是半损坏的(提示:13*7 是一个常数)。

    【讨论】:

    • 糟糕,我还没有真正完成课程,我会检查 GetHashCode。 =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多