我想在下拉列表中获取所选项目的值。我使用以下代码将文件保存到数据库中:
public ActionResult UploadDoc(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
if (file != null && file.ContentLength > 0)
{
byte[] data = new byte[file.ContentLength];
file.InputStream.Read(data, 0, file.ContentLength);
Document doc = new Document
{
UploadedOn = DateTime.Now,
MimeType = file.ContentType,
UserName = User.Identity.Name,
Data = data,
FromLanguage = 1,
ToLanguage = 2
};
dbContext = new MedicalDb();
dbContext.Documents.Add(doc);
dbContext.SaveChanges();
}
}
return RedirectToAction("Index");
}
但是,我还希望从下拉列表中获取所选值,以便我可以填充文档的FromLanguage和ToLanguage属性。我想我需要一个viewmodel,但不知道该怎么做。使用jQuery添加用于文档上载的新行,并且ddls的名称是“ddlFromLanguage1”,“ddlFromLanguage2”,“ddFromLanguage3”,“ddlToLanguage1”,“ddlToLanguage2”,“ddlToLanguage3”等。预先感谢您的任何帮助。< / p>
<form action="UploadDoc" method="post" enctype="multipart/form-data">
<table id="tblUploadDocs">
<tr id="row1">
<td><input type="file" name="files" id="file1" /></td>
<td>Bu dilden</td>
<td>@Html.DropDownList("ddlFromLanguage1", ViewBag.Languages as SelectList)</td>
<td>şu dile çevrilecek</td>
<td>@Html.DropDownList("ddlToLanguage1", ViewBag.Languages as SelectList)</td>
</tr>
</table>
<br />
<a href="javascript:addRow();" style="margin:10px 0;">Yeni dosya ekleyin</a>
<input type="submit" />
</form>
答案 0 :(得分:1)
我认为你需要看看好的例子并做同样或非常相似的事情。
看看这些:
这些应该让你去。
如果你没有成功,或者我给你的东西实际上有帮助,请告诉我。
由于
答案 1 :(得分:1)
除了与模型相关的值之外,任何回发的表单都会将FormCollection返回给控制器。
例如
//In your view
@using (Html.BeginForm("CountrySelect", "Country", FormMethod.Post))
{
@Html.AntiForgeryToken()
<select name="country" id="country-select">
<option value="selector">Pick a Country</option>
<option value="England">England</option>
<option value="England">England</option>
</select>
}
//In controller
//This will get you the name of the selected country from your form
[HttpPost]
Public ActionResult CountrySelect(FormCollection formData)
{
string country = formData["country"].toString();
}
答案 2 :(得分:0)
解决方案:
viewmodel:
public class CustomerDocUploadViewModel
{
public HttpPostedFileBase File { get; set; }
public int FromLanguage { get; set; }
public int ToLanguage { get; set; }
}
观点:
@model IList<Models.ViewModels.CustomerDocUploadViewModel>
...
<form action="UploadDoc" method="post" enctype="multipart/form-data">
<table id="tblUploadDocs">
<tr id="row1">
<td><input type="file" name="[0].File" /></td>
<td>Bu dilden</td>
<td>@Html.DropDownList("[0].FromLanguage", ViewBag.Languages as SelectList)</td>
<td>şu dile çevrilecek</td>
<td>@Html.DropDownList("[0].ToLanguage", ViewBag.Languages as SelectList)</td>
</tr>
</table>
<br />
<a id="lnkAdd" href="javascript:addRow();" style="margin:10px 0;">Yeni dosya ekleyin</a>
<input type="submit" />
</form>
最后是控制器中的action方法:
[HttpPost]
public ActionResult UploadDoc(IList<CustomerDocUploadViewModel> docInfos)
{
for (int i = 0; i < docInfos.Count; i++)
{
if (docInfos.ElementAt(i).File != null && docInfos.ElementAt(i).File.ContentLength > 0)
{
byte[] data = new byte[docInfos.ElementAt(i).File.ContentLength];
docInfos.ElementAt(i).File.InputStream.Read(data, 0, docInfos.ElementAt(i).File.ContentLength);
// Save the file into the database
Document doc = new Document
{
UploadedOn = DateTime.Now,
MimeType = docInfos.ElementAt(i).File.ContentType,
UserName = User.Identity.Name,
Data = data,
FromLanguage = docInfos.ElementAt(i).FromLanguage,
ToLanguage = docInfos.ElementAt(i).ToLanguage
};
dbContext = new MedicalDb();
dbContext.Documents.Add(doc);
dbContext.SaveChanges();
}
}
return RedirectToAction("Index");
}