我已經搜索了不少頁面等,找不到任何東西來真正幫助我。我有一個加載的視圖模型,然後我需要從選定的「DocumentTypeList」中獲取ID,以便將其分配給Document對象上的DocumentTypeId。如何從@ Html.DropDownListFor獲取所選項目
我必須將「DocumentTypeList」放在我的Document類上,還是保持它在視圖模型中的位置?
這是我的視圖模型。
#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
這是我的觀點是,在網頁
@Html.DropDownListFor(x => x.DocumentTypeList, new SelectList(Model.DocumentTypeList, "Code", "Description"), "-- Please Select --")
在視圖模型,這裏是我打電話
[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
我已經找到了行動解決方案的工作。感謝您的所有意見。
@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))
}))
你想在SaveFile中下拉選定的值操作? –
@ JcMey3r你解決了這個問題嗎? –