简体   繁体   English

如何在asp.net mvc 4中设置下拉列表的选定值

[英]How can I set the selected value of dropdownlist in asp.net mvc 4

I have the following code in the controller: 我在控制器中有以下代码:

public ActionResult method(int? id){
    var list;
    var project;
    if(id.HasValue){
       list = repository.FindAsync(Identity.User.Id);
       project = list.FirstOrDefault(p => p.Id == id);
       ViewBag.List = list;
       ViewBag.SelectedValue = project.Id;
    }
    return View();
}

And I have this in the view 我认为这是

<div>
  @Html.DropDownList("projectLists", ViewBag.list as List<SelectListItem>, new { @class = "class"})
</div>

How can I use the ViewBag.SelectedValue to render the dropdown with the project that have that id. 如何使用ViewBag.SelectedValue渲染具有该ID的项目的下拉列表。 I need a little help for this because I am new in ASP.NET MVC 我需要一点帮助,因为我是ASP.NET MVC的新手

将其设为DropDownListFor,并在其模型上具有SelectedItemID属性以将其发布到。

You need to start with a view model to represent what you want to display/edit 您需要从一个视图模型开始,以代表您想要显示/编辑的内容

public class MyViewModel
{
  public int SelectedProject { get; set; }
  public SelectList ProjectList { get; set; }
}

Then in the GET method 然后在GET方法中

public ActionResult method(int? id)
{
  IEnumerable<Project> projects = repository.FindAsync(Identity.User.Id);
  MyViewModel model = new MyViewModel()
  {
    SelectedProject = projects.FirstOrDefault(p => p.Id == id),
    ProjectList = new SelectList(projects, "Id", "Name")
  };
  return View(model);
}

And in the view (note if the value of SelectedProject matches one of the values of the options, then that option will be selected when the view is rendered) 并且在视图中(请注意,如果SelectedProject的值与选项的值之一匹配,则在呈现视图时将选择该选项)

@model yourAssembly.MyViewModel
@using (Html.BeginForm())
{
  @Html.DropDownListFor(m => m.SelectedProject, Model.ProjectList, "-Please select-", new { @class = "class" })
  <input type="submit" ../>
}

and the POST method 和POST方法

public ActionResult method(MyViewModel model)
{
  // model.SelectedProject contains the ID of the selected project
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取DropDownList选定的值,并将其发送到ASP.NET MVC 4中的控制器并使用JavaScript? - How can I get a DropDownList selected value and send it to the controller in ASP.NET MVC 4 and using JavaScript? 设置在ASP.NET MVC中的“编辑”期间选择的下拉列表值 - Set dropdownlist value selected during Edit in ASP.NET MVC 如何在asp.net mvc中为html.DropDownList设置值? - how to set a value for the html.DropDownList in asp.net mvc? 如何在 ASP.NET 的 DropDownList 中设置选定的等于 True - How can I set selected equals to True in a DropDownList in ASP.NET 如何将我在中继器中选择的 DropDownList 值添加到 ASP.Net 中的列表框控件 - How Can add I selected value of DropDownList inside a Repeater to the listbox control in ASP.Net 在下拉列表中查找项目并将其设置为.asp.net MVC 4中的选定项 - find item in dropdownlist and set it to selected in .asp.net MVC 4 将dropdownlist的选定值作为Model asp.net MVC的属性传递 - Passing selected value of dropdownlist as property of Model asp.net mvc Asp.Net MVC下拉列表选择的值消失 - Asp.Net MVC dropdownlist selected value dissappears 选定的DropDownList值未发送到ASP.NET MVC控制器 - Selected DropDownList Value Not Sending to ASP.NET MVC Controller 获取DropDownList的选定值。 Asp.NET MVC - Get the selected value of a DropDownList. Asp.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM