从模型显示时在DropDownListFor中设置默认值

时间:2019-07-03 18:25:12

标签: c# asp.net-mvc razor

如何在此@ Html.DropDownListFor中使空白默认显示为“”。

我已经尝试了超越,但它们对此不起作用。

HTML:

<td>@Html.DropDownListFor(o => o.TerminalsDDL, Model.TerminalsDDL, new { id = "ddlTerminalID", @class = "form-control align-middle" })</td>

控制器:

    public ActionResult Index()
    {
        var model = TCBL.GetTerminalData();
        return View(model);
    }

    //POST: TerminalCommand/Index
    /*This function will insert a user selecter terminal command into the TerminalCommand table*/
    public ActionResult AddTerminalCommand(AddTerminalCommandVM input)
    {

        TerminalCommand terminalCommand = new TerminalCommand();
        terminalCommand.TerminalID = input.TerminalID;
        terminalCommand.Command = input.CommandID;
        terminalCommand.CommandValue = input.CommandValue;

        TCBL.AddTerminalCommand(terminalCommand);

        var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "TerminalCommand");
        return Json(new { Url = redirectUrl });
    }

数据层:

 /*Gets All termianls for the terminalsDDL and all terminal Cmds for Model.TerminalCommands*/
    public TerminalCommandVM GetTerminalData()
    {
        TerminalCommandVM terminals = new TerminalCommandVM();

        //For Terminal drop downs
        terminals.TerminalsDDL = TCDA.GetTerminalsDropDown();

        //For terminal cmd lists 
        terminals.TerminalCommands = TCDA.GetAll();

        //For helpdescription
        terminals.HelpDescriptions = TCDA.GetAllHelpDescriptionValues();
        terminals.HelpDescriptionID = TCDA.GetAllHelpDescriptionIDs();

        //For TerminalCommandLookupsDDL
        List<SelectListItem> terminalCommandLookups = new List<SelectListItem>();

        var terminalCommandLookupsResults = TCDA.GetAllTerminalCommandLookups().OrderBy(o => o.Name); //.Where(x => x.Name.Contains("S3"));

        if (terminalCommandLookupsResults != null)
        {
            foreach (var item in terminalCommandLookupsResults)
            {
                SelectListItem newItem = new SelectListItem();
                newItem.Text = item.Name;
                newItem.Value = item.ID.ToString();
                terminalCommandLookups.Add(newItem);
            }
        }

        var terminalCommandValues = TCDA.GetAllTerminalCommandValues();
        terminals.TerminalCommandValues = terminalCommandValues;

        terminals.TerminalCommandLookupsDDL = terminalCommandLookups;

        return terminals;
    }

最下面是数据访问层,CA在其中获取要显示的数据。我相信HTML应该有某种默认的空白选择。.

2 个答案:

答案 0 :(得分:1)

您可以在数据层中的for循环之前添加空白默认值

orgText = """
\t\tAll those moments
\t\twill be lost
  in time
  like tears
in rain.
       It's time to die."""
option = QtGui.QTextOption()
option.setFlags(QtGui.QTextOption.ShowTabsAndSpaces | QtGui.QTextOption.ShowLineAndParagraphSeparators)
self.teOrg.setPlainText(orgText)
self.teOrg.document().setDefaultTextOption(option)

答案 1 :(得分:0)

您可以使用Dropdownlistfor的重载-

Html.DropDownListFor(Expression<Func<dynamic,TProperty>> expression, IEnumerable<SelectLestItem> selectList, string optionLabel, object htmlAttributes)

像这样

<td>@Html.DropDownListFor(o => o.TerminalsDDL, Model.TerminalsDDL,"", new { id = "ddlTerminalID", @class = "form-control align-middle" })</td>