C# 从 Html.DropdownListFor....MVC3 获取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11034892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:13:00  来源:igfitidea点击:

Get the text from Html.DropdownListFor....MVC3

c#asp.net-mvc-3

提问by BoundForGlory

I have a model:

我有一个模型:

public class DocumentModel
{
    public int TypeID { get; set; }
    public List<SelectListItem> DocumentTypes { get; set; }
}

I have a view:

我有一个观点:

  @Html.DropDownListFor(x => x.TypeID, Model.DocumentTypes, "- please select -")

I populate my drop down

我填充我的下拉菜单

        var model = new DocumentModel();
        model.DocumentTypes = GetDocumentTypes(); 

private static List<SelectListItem> GetDocumentTypes()
    {

        var items = new List<SelectListItem>
                        {
                            new SelectListItem
                                {Text = @"Text #1", Value = "1"},
                            new SelectListItem
                                {Text = @"Text #2", Value = "2"},
                        };

        return items;

    }

I have a controller action when the form is posted back:

当表单回发时,我有一个控制器操作:

 [HttpPost] 
    public void UploadDocument(DocumentModel model)
    {
        if (ModelState.IsValid)
        {
            // I want to get the text from the dropdown
        }
    }

How do i get the text from my drop down list? Thanks

如何从下拉列表中获取文本?谢谢

采纳答案by Shyju

You may not get this easily with the default model binding. You have to a small workaround like this.

使用默认模型绑定可能无法轻松实现这一点。你必须像这样一个小的解决方法。

1)Add a new property to your model/viewmodel to store the selected text

1)向您的模型/视图模型添加一个新属性以存储所选文本

public class DocumentModel
{
    public int TypeID { get; set; }
    public List<SelectListItem> DocumentTypes { get; set; }
    public string SelctedType { set;get;}
}

2)Use Html.HiddenForHelper method to create a hidden variable in the form for this property

2)使用Html.HiddenForHelper方法为这个属性在表单中创建一个隐藏变量

@Html.HiddenFor(x => x.SelctedType)

3)Use little javascript to override the submit ! ie; When user submits the form, Get the selected Text from the dropdown and set that value as the value of the Hidden field.

3)使用小javascript覆盖提交!IE; 当用户提交表单时,从下拉列表中获取选定的文本并将该值设置为隐藏字段的值。

$(function () {
    $("form").submit(function(){
        var selTypeText= $("#TypeID option:selected").text();
        $("#SelctedType").val(selTypeText);           
    });
});

Now in your HTTPPostaction method, This will be available in the SelectedTypeproperty.

现在在您的HTTPPost操作方法中,这将在SelectedType属性中可用。

[HttpPost]
public void UploadDocument(DocumentModel model)
{
   if(ModelState.IsValid)
   {
      string thatValue=model.SelectedType;
   }
}

回答by Behnam Esmaili

if what you want to do is to retrieve selected item then this can do the work :

如果您想要做的是检索所选项目,那么这可以完成以下工作:

  var selecteItem = model.DocumentTypes.Where(item=>item.Selected).FirstOrDefault();

Cheers!

干杯!

回答by Ctrl_Alt_Defeat

On your model I would have another string -

在你的模型上,我会有另一个字符串 -

public string Selected{ get; set; }

then in your view :

那么在你看来:

@Html.DropDownListFor(model => model.Selected, new SelectList(Model.DocumentTypes, "Value", "Text"))

回答by SimonJohnson

I stumbled here trying to find the way to get the text value out of a SelectList to display it in a format other than a DropDownList (I'm reusing my Edit ViewModel as it has all the data I required)

我在这里偶然发现了从 SelectList 中获取文本值的方法,以便以 DropDownList 以外的格式显示它(我正在重用我的 Edit ViewModel,因为它具有我需要的所有数据)

var text = selectList.Where(q => q.Selected == true).First().Text;