C# MVC 4-用于使用ID的DropDownList-检索回发值
我是MVC4新手,所以这可能是一个新手的错误。我无法将下拉列表绑定到我的模型。我试图显示用户正在使用DropDownListFor从dropdownlist中进行的选择。请参阅以下代码: CostsPerSqFoot模型:C# MVC 4-用于使用ID的DropDownList-检索回发值,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我是MVC4新手,所以这可能是一个新手的错误。我无法将下拉列表绑定到我的模型。我试图显示用户正在使用DropDownListFor从dropdownlist中进行的选择。请参阅以下代码: CostsPerSqFoot模型: public class CostsPerSqFoot { public List<Prices> RatesList { get; set; } public CostsPerSqFoot() { RatesList =
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}
};
}
}
CostDetails模型(我在其中创建SelectListItem列表):
在我的索引视图中,我有以下表单,其中包含dropdownlist,该视图使用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 floorringcalculator.mvc.Models.CostDetails
//几行HTML
@使用(Html.BeginForm(“计算”、“主页”、FormMethod.Post))
{
@Html.ValidationSummary()
@LabelFor(m=>m.Measurements.Length)
@TextBoxFor(m=>m.Measurements.Length,新的{placeholder=“Enter Length”})
@LabelFor(m=>m.Measurements.Width)
@TextBoxFor(m=>m.Measurements.Width,新的{placeholder=“Enter Width”})
@LabelFor(m=>m.Costs)
@DropDownListFor(m=>m.Id,Model.Costs,“-选择费率-”)
算计
}
最后,我只想显示用户在单击“提交”时选择的下拉选项。关于“计算”观点,我只有以下几点:
@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 floorringcalculator.mvc.Models.CostDetails
@{
布局=空;
}
计算
@模型.成本
我可能做错了什么?提前感谢。如果Id是记录Id,我将在您的模型中创建一个选定字段。然后你可以做类似的事情
@Html.DropDownListFor(x => x.Selected, Model.Costs).
所选字段将填充所选记录的值。如果需要所选文本,则可以查询Model.Costs或使用jquery。Model.Costs是列表。在DropDownList for中,您正在尝试将所选项目添加到id字段。如果您想查看所选项目,您需要查看该字段谢谢您的回答。但是我怎么能做到呢?如果我在“Calculations”视图上尝试@Html.DisplayFor(m=>m.Id),它会显示Id值,而不是下拉列表中选择的值。您好,尽管做了更改,我仍然有相同的问题。我认为我的模型没有正确地绑定到我的下拉列表中。如果可能的话,我不想使用jQuery。还有其他建议或是一个简短的演示吗?这样你就不会在下拉列表中看到任何东西了?
@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>
@Html.DropDownListFor(x => x.Selected, Model.Costs).