【问题标题】:Passing Values from the SelectList Items Using ViewModel使用 ViewModel 从 SelectList 项中传递值
【发布时间】:2014-05-29 17:35:22
【问题描述】:

我想在下拉列表中获取所选项目的值。我正在使用以下代码将文件保存到数据库中:

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 属性。我想我需要一个视图模型,但不知道该怎么做。文档上传的新行是使用 jQuery 添加的,ddl 的名称是“ddlFromLanguage1”、“ddlFromLanguage2”、“ddFromLanguage3”和“ddlToLanguage1”、“ddlToLanguage2”、“ddlToLanguage3”等。在此先感谢您的帮助。

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

【问题讨论】:

  • 我觉得你的问题不够清楚,还要检查Here 以使用带有选择列表的视图模型...

标签: asp.net-mvc viewmodel html.dropdownlistfor selectlist


【解决方案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】:

    我认为你需要看一个很好的例子,并做与他们相同或非常相似的事情。

    看看这些:

    这些应该能让你继续前进。

    如果你没有成功或者我给你的东西是否真的有帮助,请告诉我。

    谢谢

    【讨论】:

      【解决方案3】:

      解决办法:

      视图模型:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 2022-06-10
        • 2018-02-03
        • 1970-01-01
        • 2016-07-07
        • 2023-03-27
        • 1970-01-01
        相关资源
        最近更新 更多