【问题标题】:Read The Data From DropDown in ASP.Net MVC3从 ASP.Net MVC3 中的 DropDown 读取数据
【发布时间】:2012-05-12 05:42:19
【问题描述】:

我正在使用 Database First 方法执行 ASP.net MVC 3(空类型而不是 Internet 类型)...

我需要的是

第 1 步: 我只是使用下拉列表来显示公司所在的各个位置。列表来自Organization表,Location只是这个Oranization Table中的一个字符串字段,

第 2 步: 当用户进行注册时,下拉列表将显示位置。现在,用户选择印度,然后这个值(位置名称)应该存储在 UserLogin 表中......

现在如何从下拉列表中读取值,希望您能理解我的问题并提前致谢

【问题讨论】:

  • 你能展示一些你试过的代码吗?如果您有一个强类型视图,那么当您使用@Html.DropDownListFor()(如果我没记错的话)创建下拉列表时,它将处理将值传递回发布数据时使用的控制器。
  • // 在我的注册控制中,我这样写 public ActionResult Index() { ViewBag.OLocation = new SelectList(dbcontext.Organization_Details, "OName", "OLocation");返回视图(); } // 在它的视图中,我是这样写的... // 这个视图是强类型的“UserLogin”视图模型 // 之前的代码部分 @Html.Dropdownlist("OLocation")
  • 到目前为止,没有任何 cmets 或回复.....

标签: asp.net-mvc-3


【解决方案1】:

我会使用视图模型:

public class RegisterViewModel
{
    public string LocationName { get; set; }
    public IEnumerable<SelectListItem> Locations { get; set; }
}

然后是为视图提供服务的控制器操作:

public ActionResult Index()
{
    var model = new RegisterViewModel();
    model.Locations = new SelectList(dbcontext.Organization_Details, "OName", "OLocation");
    return View(model);
}

然后是对应的强类型视图:

@model RegisterViewModel
@using (Html.BeginForm())
{
    @Html.LabelFor(x => x.LocationName)
    @Html.DropDownListFor(x => x.LocationName, Model.Locations)
    <button type="submit">OK</button>
}

最后是提交表单时将调用的控制器操作:

[HttpPost]
public ActionResult Index(RegisterViewModel model)
{
    // model.LocationName will contain the selected location here
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 2012-05-09
    相关资源
    最近更新 更多