burakk
burakk

Reputation: 1291

Passing Values from the SelectList Items Using ViewModel

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>

Upvotes: 0

Views: 2050

Answers (3)

Display Name
Display Name

Reputation: 4732

I think you need to look at good example and do the same or very similar to them.

Take a look at these:

These should get you going.

Please let me know if you don't succeed or if what I gave you was actually helpful.

Thanks

Upvotes: 1

burakk
burakk

Reputation: 1291

The solution:

The viewmodel:

public class CustomerDocUploadViewModel
{
    public HttpPostedFileBase File { get; set; }
    public int FromLanguage { get; set; }
    public int ToLanguage { get; set; }
}

The view:

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

and finally the action method in the controller:

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

Upvotes: 0

krilovich
krilovich

Reputation: 3505

Any form that is posted back returns a FormCollection to the controller in addition to model related values.

For example

  //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();
}

Upvotes: 1

Related Questions