从DropDownList获取值

时间:2017-03-19 17:46:40

标签: c# asp.net asp.net-mvc razor

我有简单的网站

controller.cs

public ActionResult Dodaj()
    {

        EduSiatkiEntities daneProgramu = new EduSiatkiEntities();

        SelectList profileHasel = new SelectList(daneProgramu.SYS_PROFILE_HASEL.Select(e => new{ Value = e.idProfiluHasla, Text = e.nazwaProfilu,}), "Value", "Text");
        ViewBag.profileHasel = profileHasel;
        ViewBag.CzyZapisanoDane = "nie";

        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Dodaj(Models.UzytkowniDodajkViewModel uzytkownikForm)
    {
        Debug.WriteLine(uzytkownikForm.idProfilHasla.Where(x => x.Selected=true).FirstOrDefault().Value.ToString());
        if (ModelState.IsValid)
        {
            /**/
        }
        else
        {
            ViewBag.CzyZapisanoDane = "nie";
        }

        return View(uzytkownikForm);
    }

view.cshtml

@Html.DropDownListFor(a => a.idProfilHasla, (SelectList)@ViewBag.profileHasel, "Wybierz opcje...", new { @class = "form-control" })

model.cs

[Display(Name = "Profil hasła użytkownika")]
    [Required(ErrorMessage = "Wybierz profil hasła użytkownika")]
    public IEnumerable<SelectListItem> idProfilHasla { get; set; }

当我调试程序时,我总是从DropDownList获取NULL值(来自下面的代码)

Debug.WriteLine(uzytkownikForm.idProfilHasla.Where(x => x.Selected=true).FirstOrDefault().Value.ToString());

1 个答案:

答案 0 :(得分:0)

import sys
from PySide.QtCore import *
from PySide.QtGui import *

class M_SCROLL_GRID_WIDGET(QWidget):
    # This is a common ui class .
    def __init__(self,column_count):
        super(M_SCROLL_GRID_WIDGET, self).__init__()

        self.column_count = column_count

        self.visible_widget_list = []

        self.scroll_widget = QWidget()

        self.scroll_area = QScrollArea()
        self.scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.scroll_area.setWidgetResizable(True)
        self.scroll_area.setWidget(self.scroll_widget)

        self.main_layout = QGridLayout()

        self.main_layout.setSpacing(0)

        self.scroll_widget.setLayout(self.main_layout)
        self.main_layout.setAlignment(Qt.AlignTop)
        master_layout = QVBoxLayout(self)
        master_layout.addWidget(self.scroll_area)

        self.setLayout(master_layout)

        self.setAttribute(Qt.WA_TranslucentBackground, True)
        self.setStyleSheet("background-color:rgba(40,40,150,150);border:0px;")


    def Get_Widget_Count(self):
        return len(self.visible_widget_list)


    def Add_Widget_To_Vis_List(self, widget):
        self.visible_widget_list.append(widget)


    def Remove_Widget_To_Vis_List(self, widget):
        self.visible_widget_list.remove(widget)


    def Remove_All_Widgets(self):
        for i in reversed(range(self.main_layout.count())):
            widget = self.main_layout.itemAt(i).widget()
            widget.setParent(None)


    def Clean_Visible_List(self):
        self.visible_widget_list = []


    def Refresh(self):

        for widget_index in xrange(len(self.visible_widget_list)):
            r_position =  widget_index      / self.column_count
            c_position = (widget_index + 1) % self.column_count
            if c_position == 0:
                c_position = self.column_count
                         self.main_layout.addWidget(self.visible_widget_list[widget_index], r_position, c_position)

if __name__ == '__main__':

    app = QApplication(sys.argv)

    caofan = M_SCROLL_GRID_WIDGET(3)

    btn_list = []

    caofan.Remove_All_Widgets()
    for a in xrange(50):
        btn = QPushButton(str(a+1))
        btn_list.append(btn)
        caofan.Add_Widget_To_Vis_List(btn)

    caofan.Refresh()

    caofan.show()

    app.exec_()

对于上面的DropDownList, idProfilHasla 应该是 string 类型,因为DropDownList选择的值是一个字符串。< / p>

@Html.DropDownListFor(a => a.idProfilHasla, (SelectList)@ViewBag.profileHasel, 
     "Wybierz opcje...", new { @class = "form-control" })

如果您想将SelectList放在模型中而不是ViewBag中,您可能需要阅读this回答。