mvc 2 中的编辑模式下下拉列表不显示所选值

发布于 2024-11-16 13:43:22 字数 2565 浏览 2 评论 0原文

您好,我试图在编辑部分的下拉列表上显示数据库值,但下拉列表显示以下默认设置值是我的代码: 控制器:

 public ActionResult Edit(int id)
    {
       // Product helmet = new Product();//
        //Product garrage = new Product();

        ViewBag.mode = "edit";
        // for dropdown track
        ITrackRepository trackResp = new TrackRepository();
        IQueryable<Object> tracks = trackResp.GetVenuesSelectlist();
        ViewData["Venue"] = new SelectList(tracks, "VenueID", "Name");

        // for dropdown for event type
        ITrackdayRepository trackdayResp = new TrackdayRepository();
        IQueryable<EventType> eventTypes = trackdayResp.GetAllEventTypes();
        ViewData["EventTypes"] = new SelectList(eventTypes, "ID", "Name");

        // for dropdown experience
        IExperienceLevelRepository expLevelResp = new ExperienceLevelRepository();
        IQueryable<ExperienceLevel> expLevel = expLevelResp.GetAllExperienceLevels().OrderBy(ExperienceLevel => ExperienceLevel.Name);
        ViewData["Experience"] = new SelectList(expLevel, "ID", "Name");

        // dropdown for helmets
        IProductRepository prodResp = new ProductRepository();
        Product productQuantity = prodResp.GetProd(id);

        if (productQuantity.ProductTypeID == 1)
        {
            // dropdown for attendees
            var attendees = Enumerable.Range(1, 80).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
            ViewData["attendees1"] = new SelectList(attendees, "Value", "Text",**productQuantity.QtyAvailable)**; //productQuantity.QtyAvailable is the value from db(selected value of dropdown)

            ViewData["txtAttendees"] = productQuantity.UnitCost;
        }


        else
        {
            var emptyattendees = Enumerable.Range(1, 80).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
            ViewData["attendees1"] = new SelectList(emptyattendees.ToList(), "Value", "Text");


        }    Event trackday = trackdayResp.GetEvent(id); //returns all the values from event table whose eventid is id

        //need to return product quantity, value to drop downlist
       return View("Create", trackday);

    }

查看已编辑(工作):

  <% if (ViewBag.mode != "edit")
                  { %>

                <%: Html.DropDownList("attendees1", ViewData["attendees1"] as SelectList, "--select--")%>
               <%}else{%>
               <%: Html.DropDownList("attendees1")%>
               <%} %>

Hi I am trying to display the database value on the dropdownlist in the edit section, but the drop down list shows the default set value below is my code:
Controller:

 public ActionResult Edit(int id)
    {
       // Product helmet = new Product();//
        //Product garrage = new Product();

        ViewBag.mode = "edit";
        // for dropdown track
        ITrackRepository trackResp = new TrackRepository();
        IQueryable<Object> tracks = trackResp.GetVenuesSelectlist();
        ViewData["Venue"] = new SelectList(tracks, "VenueID", "Name");

        // for dropdown for event type
        ITrackdayRepository trackdayResp = new TrackdayRepository();
        IQueryable<EventType> eventTypes = trackdayResp.GetAllEventTypes();
        ViewData["EventTypes"] = new SelectList(eventTypes, "ID", "Name");

        // for dropdown experience
        IExperienceLevelRepository expLevelResp = new ExperienceLevelRepository();
        IQueryable<ExperienceLevel> expLevel = expLevelResp.GetAllExperienceLevels().OrderBy(ExperienceLevel => ExperienceLevel.Name);
        ViewData["Experience"] = new SelectList(expLevel, "ID", "Name");

        // dropdown for helmets
        IProductRepository prodResp = new ProductRepository();
        Product productQuantity = prodResp.GetProd(id);

        if (productQuantity.ProductTypeID == 1)
        {
            // dropdown for attendees
            var attendees = Enumerable.Range(1, 80).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
            ViewData["attendees1"] = new SelectList(attendees, "Value", "Text",**productQuantity.QtyAvailable)**; //productQuantity.QtyAvailable is the value from db(selected value of dropdown)

            ViewData["txtAttendees"] = productQuantity.UnitCost;
        }


        else
        {
            var emptyattendees = Enumerable.Range(1, 80).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
            ViewData["attendees1"] = new SelectList(emptyattendees.ToList(), "Value", "Text");


        }    Event trackday = trackdayResp.GetEvent(id); //returns all the values from event table whose eventid is id

        //need to return product quantity, value to drop downlist
       return View("Create", trackday);

    }

View Edited(WOrking):

  <% if (ViewBag.mode != "edit")
                  { %>

                <%: Html.DropDownList("attendees1", ViewData["attendees1"] as SelectList, "--select--")%>
               <%}else{%>
               <%: Html.DropDownList("attendees1")%>
               <%} %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

原来是傀儡 2024-11-23 13:43:22

一个月前我遇到了同样的问题,我通过这样做解决了它:

ViewData["attendees1"] = new SelectList(attendees, "Value", "Text", productQuantity.QtyAvailable);

我的意思是,您必须使用 SelectedValue 添加第四个参数,您可以在编辑之前从原始值中获取它。您只需在“编辑”操作中执行此操作,无需在“创建”中执行此操作,因为它是一个新对象并且尚未选择任何值。

在您的标记中,您可以像这样定义 DropDownList:

<%: Html.DropDownList("attendees1") %>

这样,将选择所选值而不是默认值。

希望有帮助。

编辑:
创建操作方法:

ViewData["attendees1"] = new SelectList(attendees, "Value", "Text");

编辑操作方法:

ViewData["attendees1"] = new SelectList(attendees, "Value", "Text", productQuantity.QtyAvailable);

在创建和编辑视图中进行标记

<%: Html.DropDownList("attendees1") %>

I had the same problem a month ago, and I solved it by doing this:

ViewData["attendees1"] = new SelectList(attendees, "Value", "Text", productQuantity.QtyAvailable);

I mean, you have to add a 4th parameter with the SelectedValue which you take it from the original value before the edit. You have to do this only in Edit action, no need to do that in Create since it is a new object and no value is selected yet.

And in your markup you define the DropDownList like this:

<%: Html.DropDownList("attendees1") %>

This way the selected value will be selected instead of the default one.

Hope that helps.

EDIT:
Create action method:

ViewData["attendees1"] = new SelectList(attendees, "Value", "Text");

Edit action method:

ViewData["attendees1"] = new SelectList(attendees, "Value", "Text", productQuantity.QtyAvailable);

Markup in both Create and Edit views

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