0

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.

Must I put the "DocumentTypeList" on my Document class or keep it where it is in the view model?

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))
                }))
tereško
  • 58,060
  • 25
  • 98
  • 150
JcMey3r
  • 181
  • 2
  • 14

2 Answers2

0

Create another property in your ViewModel which gets the selectedValue.

public DocumentType SelectedDocumentType {get; set;}

Change your DropDownList like so

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

The SelectedValue can be accessed using SelectedDocumentType property.

RKS
  • 1,370
  • 11
  • 20
0

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.

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:

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?

You should not use a generic collection in your helper to get the selected item, for the aforementioned reasons.

Community
  • 1
  • 1
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61