ASP.NET MVC dropdown-list from database

后端 未结 6 1461
梦如初夏
梦如初夏 2020-12-05 05:30

Ok, so I\'m new to this whole MVC-world, but it seems to be a pretty good way of getting things done and I\'m trying to make it work here.

The problem is: I can\'t g

6条回答
  •  情话喂你
    2020-12-05 05:54

    this is my table in the database

    take look it my Action controller

        // GET: Letters
        public ActionResult Index()
        {
            ViewBag.LetterStatus = new SelectList(LetterStatusService.GetAllLettersStatus(), "Id", (CultureHelper.GetCurrentCulture() == "ar") ? "NameArabic" : "Name", Request.QueryString["LetterStatus"]);
            return View();
        }
    

    and in the view

      @Html.DropDownList("LetterStatus")
    

    the constructor I used is

    new SelectList(
        list myListFromDatabase,
        string PropertyNameOfValueInHtml,
        string PropertyNameOfDesplayInHtml,
        string SelectedItemValue 
    );
    

    this line Request.QueryString["LetterStatus"] because I send the Selected Items within QuerySrting

    and based on CurrentCulture I chose what column to display

    and the result are

    but I think the best way to do this,,,, is to get or create the Items then Iterate throw them to generate the select tag manually. I described this approach well in this answer

    hope this helps you

提交回复
热议问题