[英]Table value from View to Controller ( ASP.NET MVC )
我正在ASP.NET MVC 5中的一個項目上工作,我的VIEW中有一個TABLE,我在其中通過列表和Viewbag顯示ftp文件夾的內容。
我需要表格可點擊。 我使用了* .js文件,因此行是可單擊的,但是現在我將文件名(行的值)返回給Controller時遇到了問題。
查看代碼:
<table class="row col-md-12 leva table-responsive table-bordered table-hover ">
<tbody data-link="row" class="rowlink">
@foreach (var item in ViewBag.files)
{
<tr>
<td class=" row col-md-12 "><a href="#" target="_blank"> <h5> @item</h5></a></td>
</tr>
}
</tbody>
</table>
和控制器代碼:
List<string> ftpfiles = new List<string>();
try
{
var ftpco = new Project.Helpers.ftp(@path, name, passwd);
string[] simpleDirectoryListing = ftpco.directoryListSimple("");
for (int i = 0; i < simpleDirectoryListing.Count(); i++)
{
if (simpleDirectoryListing[i].Length >= 3)
{
ftpfiles.Add(simpleDirectoryListing[i]);
}
}
ViewBag.files = soubory;
}
catch { ViewBag.files = "nenacteno"; }
當用戶單擊一行時,尚不清楚交互應該是什么。 你想要:
我將安排文件下載的存根,但是您可以通過更改操作方法邏輯將其更改為所需的任何交互。 您應該做的是通過URL中的GET傳遞文件名(因為這是MVC方式)。 該視圖將是(格式缺少@,因為代碼格式化程序目前不適合我使用):
foreach (var item in ViewBag.files)
{
<tr>
<td>@Html.ActionLink(item, "Download", "YourController", new { id = item }, null)
</tr>
}
然后您將在具有get參數的控制器中創建一個動作:
//
//GET: /YourController/Download/{id}
public ActionResult Download(string id)
{
//download logic
}
通過將new { id = item }
到html helper中,可以通過GET將ftp doc名稱傳遞給action方法。 在這種情況下,您可以在邏輯中使用名為“ id”的字符串來檢索文檔,或者如果您的原始意圖不是下載文檔,則可以以類似方式實現所需的任何內容。
順便說一句,請確保使用正確的@Html.ActionLink
重載-如果您注意到我輸入了4個參數,則null為最后一個。 在MVC5中,他們對此進行了更改,如果您不輸入第4個參數,則將在URL中獲得/ controller / action?id = Value而不是/ controller / action / value。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.