如何从 ASP.NET MVC2 中的 DropDownListFor html 助手检索值?

发布于 2024-08-30 11:42:23 字数 1989 浏览 5 评论 0原文

我知道这里有一些关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜访吸血鬼 2024-09-06 11:42:23

您的视图标记中是否包含 Html.BeginFormAjax.BeginForm ?这是一种常见的疏忽,可能会导致您所指的行为。我无法从您在问题中粘贴的代码看出。干杯。

Did you include Html.BeginForm or Ajax.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文