在使用SelectList函数的时候,需要注意一下情况:

  1. 如果使用的是ViewBag来保存List, 那么这个ViewBag保存的变量名应该与你希望从用户那获取的数据的属性名一致,比如,Team 类有两个属性: TeamID, TeamName, 我希望提供给用户一个TeamName列表,然后根据用户的选择返回对应的TeamID给[HttpPost] Action.如以下代码:
            public ActionResult Index()
            {
                ViewBag.TeamID = new SelectList(DataSrcDB.Teams, "TeamID", "TeamName");
                var team = new Team();
                return View(team);
            }
    

     

  2. View中需要注意的是DropDownList函数的第一个参数应该是TeamID。
    @using(Html.BeginForm()){    
        <fieldset>
            <legend>Team</legend>
    
            <div>
                @Html.LabelFor(model => model.TeamID, "TeamIDs")
            </div>
            <div>
                @Html.DropDownList("TeamID",String.Empty)
    
            </div>
    
            <input type="submit" value="Search" name="Submit" />
        </fieldset>
        
    }
    

     

相关文章:

  • 2022-12-23
  • 2021-06-01
  • 2021-11-21
  • 2022-12-23
  • 2021-07-26
  • 2021-12-08
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-21
  • 2022-12-23
  • 2021-07-26
相关资源
相似解决方案