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))
}))