0

enter image description here

I'd like to get the values of the selected items in dropdownlists. I am saving the files into the database with the following code:

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

but, I'd also like to get the selected values from the dropdownlists so that I can populate the FromLanguage and ToLanguage properties of the documents. I guess I'd need a viewmodel, but don't know how to do it. New rows for document upload are added using jQuery and names of the ddls are "ddlFromLanguage1", "ddlFromLanguage2", "ddFromLanguage3", and "ddlToLanguage1", "ddlToLanguage2", "ddlToLanguage3", etc. Thanks in advance for any help.

<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>
4

3 回答 3

1

我认为您需要查看好的示例并与他们做相同或非常相似的事情。

看看这些:

这些应该让你继续前进。

如果您没有成功,或者我给您的内容是否真的有帮助,请告诉我。

谢谢

于 2013-03-19T18:17:37.223 回答
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();
}
于 2013-03-19T19:41:01.347 回答
0

解决方案:

视图模型:

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>

最后是控制器中的操作方法:

[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");
    }     
于 2013-03-20T09:43:44.290 回答