如何从 ASP.NET MVC2 中的 DropDownListFor html 助手检索值?
我知道这里有一些关于 DropDownListFor 的类似问题,但都没有帮助我......
我在我的项目中使用实体框架作为 ORM。有一种名为“Stete”的 EF 模型。 Stete 在 EF 模型上有名为“Drustva”的外国
现在我正在尝试为 Stete 模型制作一个用于编辑数据的表单。我设法显示所有内容,包括 Stete.Drustva.Naziv 属性,但我无法在处理程序方法 [HttpPost] 中获取最后一个属性。无论我在下拉列表中选择什么,它总是返回 0。
这是代码:
DrustvaController:
public static IEnumerable<SelectListItem> DrustvaToSelectListItemsById(this KnjigaStetnikaEntities pEntities, int Id)
{
IEnumerable<Drustva> drustva = (from d in pEntities.Drustva
select d).ToList();
return drustva.OrderBy(drustvo => drustvo.Naziv).Select(drustvo => new SelectListItem
{
Text = drustvo.Naziv,
Value = drustvo.Id.ToString(),
Selected = (drustvo.Id == Id)? true : false
});
}
SteteController:
private IEnumerable<SelectListItem> privremenaListaDrustava(int Id)
{
using (var ctx = new KnjigaStetnikaEntities())
{
return ctx.DrustvaToSelectListItemsById(Id);
}
}
public ActionResult IzmijeniPodatkeStete(Int32 pBrojStete)
{
PretragaStetaModel psm = new PretragaStetaModel();
ViewData["drustva"] = privremenaListaDrustava(psm.VratiStetuPoBrojuStete(pBrojStete).Drustva.Id);
ViewData.Model = new Models.Stete();
return View("EditView", (Stete.Models.Stete)psm.GetSteta(pBrojStete));
}
EditView:
<div class="editor-label">
<%: Html.Label("Društvo") %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(m => m.Drustva.Naziv, ViewData["drustva"] as IEnumerable<SelectListItem>) %>
<%: Html.ValidationMessageFor(model => model.FKDrustvo) %>
</div>
很抱歉没有将对象名称翻译成英文,但它们几乎没有适当的翻译。如果有必要,我可以尝试创建类似的例子......
I know there was few similar questions here about DropDownListFor, but neither helped me...
I use Entity Framework as ORM in my project. There's EF model called "Stete". Stete has Foreign on EF model called "Drustva"
Now I'm trying to make a form for editing the data, for Stete model. I managed to display everything, including Stete.Drustva.Naziv property, but I can't get this last property in my handler method [HttpPost]. It always return 0, no matter what I select in drop down list.
Here's the code:
DrustvaController:
public static IEnumerable<SelectListItem> DrustvaToSelectListItemsById(this KnjigaStetnikaEntities pEntities, int Id)
{
IEnumerable<Drustva> drustva = (from d in pEntities.Drustva
select d).ToList();
return drustva.OrderBy(drustvo => drustvo.Naziv).Select(drustvo => new SelectListItem
{
Text = drustvo.Naziv,
Value = drustvo.Id.ToString(),
Selected = (drustvo.Id == Id)? true : false
});
}
SteteController:
private IEnumerable<SelectListItem> privremenaListaDrustava(int Id)
{
using (var ctx = new KnjigaStetnikaEntities())
{
return ctx.DrustvaToSelectListItemsById(Id);
}
}
public ActionResult IzmijeniPodatkeStete(Int32 pBrojStete)
{
PretragaStetaModel psm = new PretragaStetaModel();
ViewData["drustva"] = privremenaListaDrustava(psm.VratiStetuPoBrojuStete(pBrojStete).Drustva.Id);
ViewData.Model = new Models.Stete();
return View("EditView", (Stete.Models.Stete)psm.GetSteta(pBrojStete));
}
EditView:
<div class="editor-label">
<%: Html.Label("Društvo") %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(m => m.Drustva.Naziv, ViewData["drustva"] as IEnumerable<SelectListItem>) %>
<%: Html.ValidationMessageFor(model => model.FKDrustvo) %>
</div>
I am sorry for not translating names of the objects into english, but they hardly have appropriate translation. If necessary, I can try creating similar example...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的视图标记中是否包含
Html.BeginForm
或Ajax.BeginForm
?这是一种常见的疏忽,可能会导致您所指的行为。我无法从您在问题中粘贴的代码看出。干杯。Did you include
Html.BeginForm
orAjax.BeginForm
in your view markup? That is a common oversight that can cause the behavior you are referring to. I can't tell from the code you pasted in your question.Cheers.