简体   繁体   English

ASP.NET MVC 2-如何从下拉菜单中获取选定的值?

[英]ASP.NET MVC 2 - How can I get the selected value from a drop down into my view model?

I'm creating a drop down via 我正在通过创建下拉列表

<%= Html.DropDownList("data.Language", Model.LanguageOptions) %>

and want to read back its value through automatic model binding into my LanguageModel viewmodel: 并想通过自动将模型绑定到我的LanguageModel视图模型中来读取其值:

public ActionResult Save(LanguageModel data)

However, data.Language is null when the Save method is called. 但是,调用Save方法时,data.Language为null。

How do I get the selected value from my data.Language dropdown into data.Language? 如何从我的data.Language下拉列表中获取选定的值到data.Language?

get rid of the data. 摆脱数据。

<%= Html.DropDownList("Language", Model.LanguageOptions) %>

Or try: 或尝试:

<%= Html.DropDownListFor(m => m.Language, Model.LanguageOptions) %>

(although m.Langage may not be right - it depends on how your view model is set up) (尽管m.Langage可能不正确-这取决于您如何设置视图模型)

I don't know how your controller action and model look like but this definitely works: 我不知道您的控制器动作和模型是什么样子,但这确实可以工作:

Model: 模型:

public class LanguageModel
{
    public string Language { get; set; }
    public IEnumerable<SelectListItem> LanguageOptions
    {
        get
        {
            return new[] 
            {
                new SelectListItem { Value = "en", Text = "English" },
                new SelectListItem { Value = "fr", Text = "French" },
            };
        }
    }
}

Controller: 控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new LanguageModel());
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(LanguageModel data)
    {
        return View(data);
    }
}

View: 视图:

<% using (Html.BeginForm()) { %>
    <%= Html.DropDownList("Language", Model.LanguageOptions) %>
    <input type="submit" value="OK" />
<% } %>

<div><%= Html.Encode(Model.Language) %></div>

Of course if you are using ASP.NET MVC 2.0, the strongly typed DropDownListFor helper is preferable. 当然,如果您使用的是ASP.NET MVC 2.0,则最好使用强类型的DropDownListFor帮助器。

暂无
暂无

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

相关问题 如何确定选定的值在asp.net MVC视图中的下拉列表? - How to determine selected value for drop down lists in asp.net MVC View? 如何在asp.net mvc3中获取下拉列表的选定值? - how to get selected value of drop down list in asp.net mvc3? 如何在ASP.NET MVC 3中为填充的下拉列表创建视图模型 - How do I create a view model for a populated drop down list in ASP.NET MVC 3 在用户注册视图中添加其他模型的项目的下拉视图(ASP.NET MVC5) - Adding a drop down view of items from another model in the user registration view (ASP.NET MVC5) 如何在ASP.Net MVC的视图内将项目添加到视图模型的列表中? - How can I add items to a list in my View Model within a View in ASP.Net MVC? 获取下拉列表用于在ASP.NET MVC中显示选定的值 - Get the drop downListFor show the selected value in asp.net mvc ASP.NET 代码隐藏无法获取选定的下拉列表值(设置 Javascript 属性后) - ASP.NET codebehind can't get selected drop down list value (after set Javascript attribute) 如何获取转换后的布尔值以在asp.net gridview的可编辑下拉列表中显示为选定值? - How to get converted boolean value to show as selected value in the editable drop down list in asp.net gridview? 如何从 ASP.NET Core MVC 的 razor 文件中的下拉列表中获取选定的值 - How can I get the selected value from a dropdown from within a razor file in ASP.NET Core MVC 如何获取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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM