返回文本从下拉MVC [英] Returning Text From A DropDown MVC

查看:105
本文介绍了返回文本从下拉MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉我与看起来像数据填充....

I have a dropdown I am populating with data that looks like....

  List<SelectListItem> items = new List<SelectListItem>();
        items.Add(new SelectListItem
        {
            Text = "Some Name",
            Value = "1"
        });

在下拉列表是这样

The dropdown is this

 <%: Html.DropDownList("Nick", ViewData["Nick"] as SelectList, new { @onchange = "this.form.submit()" })%>

这成功返回值属性,以我的操作方法,我想回到文本属性。我该怎么做呢?

This successfully returns the "Value" property to my action method, I wanna return the "Text" property instead. How do I do that?

推荐答案

在文本没有回来。它只返回定义的值(这是HTML的在做这个)。您可以通过简单地交换价值里面的内容是有些名称掉它,使它只返回的文本。

The 'text' isn't returned. It only returns the value by definition (this is HTML that's doing this). You could swap it so that it only returned the text by simply swapping the content inside the value to be "Some Name".

 List<SelectListItem> items = new List<SelectListItem>();
 items.Add(new SelectListItem
 {
    Text = "Some Name",
    Value = "Some Name"
 });

更有可能的是,你可能会发现,它的更好利用你送回到你的关键,简单地获得的价值。严格地说,这样的伪东西(-esque)code。

More likely, you'll probably find that it's better to simply get the value using the key you get sent back to you. Loosely speaking, something like this pseudo(-esque) code.

 [HttpPost]
 public ActionResult Edit(EditViewModel viewModel)
 {
     var listOfOptions = GetListOfOptions(viewModel.Nick);
     string selectedOptionText = listOfOptions.Text;
 }

这篇关于返回文本从下拉MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆