【问题标题】:MVC 4 - Dropdownlistfor using IDs - Retrieving postback valueMVC 4 - 使用 ID 的下拉列表 - 检索回发值
【发布时间】:2013-10-24 00:55:53
【问题描述】:

我是 MVC 4 的新手,所以这可能是新手的错误。我无法将下拉列表绑定到我的模型。我正在尝试显示用户使用 DropDownListFor 从下拉列表中所做的选择。见以下代码:

CostsPerSqFoot 模型:

public class CostsPerSqFoot
{
    public List<Prices> RatesList { get; set; }

    public CostsPerSqFoot()
    {
        RatesList = new List<Prices>()
        {
            new Prices() {Id = 1, Rate = 1.00m},
            new Prices() {Id = 2, Rate = 2.00m},
            new Prices() {Id = 3, Rate = 5.00m},
            new Prices() {Id = 4, Rate = 10.00m}
        };
    }

}

价格模型:

public class Prices
{
    public int? Id { get; set; }
    public decimal? Rate { get; set; }
}

CostDetails 模型(我在其中创建 SelectListItem 列表):

public class CostDetails
{
    public int? Id { get; set; }
    public Measurements Measurements { get; set; }

    public List<SelectListItem> Costs { get; set; }

    public CostDetails()
    {

    }

    public CostDetails(List<Prices> rates)
    {
        Costs = new List<SelectListItem>();

        foreach (var rate in rates)
        {
            Costs.Add(new SelectListItem()
            {
                Text =string.Format("{0:c}", rate.Rate), Value = rate.Id.ToString()
            });
        }


    }

}

这是我的控制器 ActionResult 方法:

public ActionResult Index()
    {
        var rates = new CostsPerSqFoot();
        var model = new CostDetails(rates.RatesList);

        return View(model);
    }

    [HttpPost]
    public ActionResult Calculations(CostDetails model)
    {
        if (ModelState.IsValid)
            return View(model);

        else
            return View("Index", model);
    }

在我的索引视图中,我有以下包含下拉列表的表单,并且该视图正在使用 CostDetails 模型:

@model FlooringCalculatorMVC.Models.CostDetails

// a few lines of HTML

@using (Html.BeginForm("Calculations", "Home", FormMethod.Post))
    {
        @Html.ValidationSummary()

        @Html.LabelFor(m => m.Measurements.Length)
        @Html.TextBoxFor(m =>m.Measurements.Length, new {placeholder = "Enter length"})
        <br/>
        <br/>

        @Html.LabelFor(m => m.Measurements.Width)
        @Html.TextBoxFor(m =>m.Measurements.Width, new {placeholder = "Enter width"})
        <br/>
        <br/>

        @Html.LabelFor(m => m.Costs)
        @Html.DropDownListFor(m =>m.Id, Model.Costs, "- Select a rate -")

        <button type="submit">Calculate</button>

    }

最后,我只想显示用户在单击“提交”时所做的选定下拉选项。在“计算”视图中,我只有以下内容:

@model FlooringCalculatorMVC.Models.CostDetails

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Calculations</title>
</head>
<body>
<div>
    @Model.Costs
</div>
</body>
</html>

我可能做错了什么?提前致谢。

【问题讨论】:

  • Model.Costs 是列表。在 DropDownListFor 您正在尝试将所选项目添加到 id 字段。如果您想查看所选项目,则需要查看该字段
  • 感谢您的回答。但我该怎么做呢?如果我在“计算”视图上尝试 @Html.DisplayFor(m=>m.Id),它会显示 Id 值,而不是下拉列表中的选定值。

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


【解决方案1】:

如果 Id 是记录 ID,我将在您的模型中创建一个选定字段。那么你可以做类似的事情

@Html.DropDownListFor(x => x.Selected, Model.Costs).  

所选字段将填充所选记录的值。如果您想要选定的文本,则可以查询 Model.Costs 或使用 jquery。

【讨论】:

  • 您好,尽管进行了更改,但我仍然遇到同样的问题。我相信我的模型没有正确绑定到我的下拉列表。如果可能的话,我宁愿不使用 jQuery。还有其他建议或简短的演示吗?
  • 所以您在下拉列表中看不到任何内容?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多