简体   繁体   English

如何从@ Html.DropDownListFor获取选定的项目

[英]How do I get the selected Item from @Html.DropDownListFor

I've searched quite a few pages etc and can't find anything to really help me. 我搜索了很多页面,等等,找不到任何真正对我有帮助的东西。 I have a view model that loads and then I need to get the ID from the selected "DocumentTypeList" so I can assign it to a DocumentTypeId on my Document object. 我有一个加载的视图模型,然后需要从选定的“ DocumentTypeList”中获取ID,以便可以将其分配给我的Document对象上的DocumentTypeId。

Must I put the "DocumentTypeList" on my Document class or keep it where it is in the view model? 我必须将“ DocumentTypeList”放在我的Document类上还是将其保留在视图模型中的位置?

Here is my view model. 这是我的视图模型。

 #region Properties
    public Document Document { get; set; }

    public List<CultureInfo> AvaialableLocales { get; set; }

    public IEnumerable<DocumentType> DocumentTypeList {get; set;}
    #endregion

    #region Constructor
    public FileUploadViewModel()
    {
        Document = new Document();

        AvaialableLocales = GTSSupportedLocales.Locales;
    }
    #endregion

here is my view that has the viewmodel on the page 这是我的视图,该视图在页面上

@Html.DropDownListFor(x => x.DocumentTypeList, new SelectList(Model.DocumentTypeList, "Code", "Description"), "-- Please Select --")

and here is the action I call 这是我叫的动作

[HttpPost]
    public ActionResult SaveFile(FileUploadViewModel Doc)
    {
        if (Request.Files.Count > 0)
        {
            var file = Request.Files[0];

            if (file != null && file.ContentLength > 0)
            {
                using (Stream inputStream = file.InputStream)
                {
                    MemoryStream memoryStream = inputStream as MemoryStream;
                    if (memoryStream == null)
                    {
                        memoryStream = new MemoryStream();
                        inputStream.CopyTo(memoryStream);
                    }
                    Doc.Document.UploadedFile = memoryStream.ToArray();
                }
            }
        }         

        return Content("File is being uploaded");
    }

UPDATE 更新

I have found a solution that works. 我找到了可行的解决方案。 Thank you for all the input. 感谢您的所有投入。

 @Html.DropDownListFor(n => n.Document.DocumentTypeId, Model.DocumentTypeList.Select(option => new SelectListItem()
                {
                    Text = option.Description,
                    Value = option.ID.ToString(),
                    Selected = (!Model.Document.DocumentTypeId.IsNull() && (Model.Document.DocumentTypeId == option.ID))
                }))

Create another property in your ViewModel which gets the selectedValue. 在ViewModel中创建另一个获取selectedValue的属性。

public DocumentType SelectedDocumentType {get; set;}

Change your DropDownList like so 像这样更改您的DropDownList

Html.DropDownListFor(n => n.SelectedDocumentType, new SelectList(DocumentTypeList, "Code", "Description"))

The SelectedValue can be accessed using SelectedDocumentType property. 可以使用SelectedDocumentType属性访问SelectedValue。

As @DarinDimitrov answered here , in ASP.NET MVC the helper DropDownListFor isn't able to determine the selected item when you're using a lambda expression as first argument when it represents complex properties with collections. 就像@DarinDimitrov 在这里回答的那样,在ASP.NET MVC中,当您使用lambda表达式作为第一个参数(当它表示带有集合的复杂属性时)时,帮助器DropDownListFor无法确定所选项目。

It's a limitation, but you should be able to use the helper, setting a simple property in your ViewModel, as the lambda expression passed as first argument in the helper, in this way: 这是一个限制,但是您应该能够使用助手,在ViewModel中设置一个简单的属性,因为lambda表达式是通过助手中的第一个参数传递的,如下所示:

public DocumentType MySelectedDocumentType {get; set;}

Then your helper declaration will be something like this: 然后,您的帮助程序声明将如下所示:

@Html.DropDownListFor(x => x.MySelectedDocumentType, new SelectList(Model.DocumentTypeList, "Code", "Description"), "-- Please Select --")

Must I put the "DocumentTypeList" on my Document class or keep it where it is in the view model? 我必须将“ DocumentTypeList”放在我的Document类上还是将其保留在视图模型中的位置?

You should not use a generic collection in your helper to get the selected item, for the aforementioned reasons. 由于上述原因,您不应在助手中使用通用集合来获取所选项目。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM