模型绑定问题
Issues with ModelBinding
我不清楚为什么这不起作用
public class Regiones
{
[DisplayName("Select the region")]
public int SelectedId { get; set; }
public IEnumerable<SelectListItem> SelectedRegion { get; set; }
public Regiones()
{
SelectedRegion = new[]{
new SelectListItem { Value = "BR", Text = "Brasil" },
new SelectListItem { Value = "EUNE", Text = "Europe East" },
new SelectListItem { Value = "EUW", Text = "Europe West" },
new SelectListItem { Value = "KR", Text = "Korea" },
new SelectListItem { Value = "LAN", Text = "Latin America North" },
new SelectListItem { Value = "LAS", Text = "Latin America South" },
new SelectListItem { Value = "NA", Text = "Northamerica" },
new SelectListItem { Value = "OCE", Text = "Oceania" },
new SelectListItem { Value = "TR", Text = "Tr" },
new SelectListItem { Value = "PBE", Text = "PBE" },
new SelectListItem { Value = "Global", Text = "Global" }
};
}
}
控制器:
public class FeaturedGamesController : Controller
{
public ActionResult Index()
{
Regiones region = new Regiones();
return View(region);
}
[HttpPost]
public ActionResult Featured(Regiones region)
{
string selectedregion = region.SelectedRegion.ElementAt(region.SelectedId).Text;
}
查看:
<div class="row">
<div class="col-md-12 text-center">
@using (@Html.BeginForm("Featured", "FeaturedGames", FormMethod.Post, new { id = "form" }))
{
@Html.AntiForgeryToken();
<div class="row">
<div class="col-md-10 col-md-offset-2">
<fieldset>
<div class="col-md-3">
@Html.LabelFor(x => x.SelectedId)
</div>
<div class="col-md-2">
@Html.DropDownListFor(x => x.SelectedId, Model.SelectedRegion, new { @id = "regiones" })
</div>
<div class="col-md-2">
<input type="submit" class="btn btn-default" value="Execute" />
</div>
</fieldset>
</div>
</div>
}
</div>
我
试过这个:
How to go to particular Item in IEnumerable and this: Pass SelectedValue of DropDownList in Html.BeginForm() in ASP.NEt MVC 3
当我点击发送时,SelectedId 中的结果始终为 0...
我可以缺少什么?提前谢谢你
问题是你的 Value
是 string
类型 SelectListItem
但你的 SelectedId
是 int 类型。
Value
下一行的类型是 string
new SelectListItem { Value = "BR", Text = "Brasil" },
selectedid
下面这行代码是 int
.
[DisplayName("Select the region")]
public int SelectedId { get; set; }
因此,价值在转换中丢失了。将 SelectedId
的数据类型更改为 string
以解决问题。
Fiddle 这里:https://dotnetfiddle.net/SDZZks
我不清楚为什么这不起作用
public class Regiones
{
[DisplayName("Select the region")]
public int SelectedId { get; set; }
public IEnumerable<SelectListItem> SelectedRegion { get; set; }
public Regiones()
{
SelectedRegion = new[]{
new SelectListItem { Value = "BR", Text = "Brasil" },
new SelectListItem { Value = "EUNE", Text = "Europe East" },
new SelectListItem { Value = "EUW", Text = "Europe West" },
new SelectListItem { Value = "KR", Text = "Korea" },
new SelectListItem { Value = "LAN", Text = "Latin America North" },
new SelectListItem { Value = "LAS", Text = "Latin America South" },
new SelectListItem { Value = "NA", Text = "Northamerica" },
new SelectListItem { Value = "OCE", Text = "Oceania" },
new SelectListItem { Value = "TR", Text = "Tr" },
new SelectListItem { Value = "PBE", Text = "PBE" },
new SelectListItem { Value = "Global", Text = "Global" }
};
}
}
控制器:
public class FeaturedGamesController : Controller
{
public ActionResult Index()
{
Regiones region = new Regiones();
return View(region);
}
[HttpPost]
public ActionResult Featured(Regiones region)
{
string selectedregion = region.SelectedRegion.ElementAt(region.SelectedId).Text;
}
查看:
<div class="row">
<div class="col-md-12 text-center">
@using (@Html.BeginForm("Featured", "FeaturedGames", FormMethod.Post, new { id = "form" }))
{
@Html.AntiForgeryToken();
<div class="row">
<div class="col-md-10 col-md-offset-2">
<fieldset>
<div class="col-md-3">
@Html.LabelFor(x => x.SelectedId)
</div>
<div class="col-md-2">
@Html.DropDownListFor(x => x.SelectedId, Model.SelectedRegion, new { @id = "regiones" })
</div>
<div class="col-md-2">
<input type="submit" class="btn btn-default" value="Execute" />
</div>
</fieldset>
</div>
</div>
}
</div>
我 试过这个: How to go to particular Item in IEnumerable and this: Pass SelectedValue of DropDownList in Html.BeginForm() in ASP.NEt MVC 3
当我点击发送时,SelectedId 中的结果始终为 0...
我可以缺少什么?提前谢谢你
问题是你的 Value
是 string
类型 SelectListItem
但你的 SelectedId
是 int 类型。
Value
下一行的类型是 string
new SelectListItem { Value = "BR", Text = "Brasil" },
selectedid
下面这行代码是 int
.
[DisplayName("Select the region")]
public int SelectedId { get; set; }
因此,价值在转换中丢失了。将 SelectedId
的数据类型更改为 string
以解决问题。
Fiddle 这里:https://dotnetfiddle.net/SDZZks