[英]Passing same parameter to an action on clicking two different buttons
我有一個顯示文件名列表的視圖。 我也有兩個按鈕,分別稱為View和Release 。 當我從列表中選擇一個文件名並單擊視圖時,它會導航到適當的操作方法以及作為參數選擇的文件名,並根據需要執行功能。
但是當我在選擇文件名后單擊“ 釋放”時,它確實導航到適當的操作方法,但沒有將文件名作為參數傳遞給操作方法。 它顯示為null
。
請注意,“ 查看和發布”直接指向具有不同操作方法的單個控制器。
單擊發布時,如何獲取文件名作為參數?
請參見下面的代碼:
public class HoldFilesController : Controller
{
// GET: HoldFiles
string holdpath = ConfigurationManager.AppSettings["HoldPath"].ToString();
public ActionResult Index()
{
DirectoryInfo dirInfo = new DirectoryInfo(holdpath);
List<FileInfo> files = dirInfo.GetFiles().ToList();
return View("Index",files);
}
}
[HttpPost]
public ActionResult ViewFile(string[] Name)
{
byte[] ImageData = null;
for (int i = 0; i < Name.Length; i++)
{
string filepath = holdpath + @"\" + Name[i];
FileStream fs = new FileStream(filepath, FileMode.Open,
FileAccess.ReadWrite);
ImageData = new byte[fs.Length];
fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
}
return File(ImageData,"application/pdf");
}
[HttpPost]
public ActionResult ReleaseFile(string[] Name)
{
for(int i=0; i<Name.Length;i++)
{
string sourcefilepath= holdpath + @"\" + Name[i];
string Destinationfilepath =
ConfigurationManager.AppSettings["ReleaseFolderPath"].ToString();
string ReleaseFilePath = Destinationfilepath + @"\" + Name[i];
if (Directory.Exists(Destinationfilepath))
{
System.IO.File.Move(sourcefilepath, ReleaseFilePath);
}
}
return RedirectToAction("Index");
}
這是我的看法的代碼:
@model IEnumerable<FileInfo>
@{
ViewBag.Title = "files";
}
<h2>Held files</h2>
@using (Html.BeginForm())
{
<div style="border:solid;width:100%;overflow-x:auto;">
<table align="center" style="width:100%">
<thead>
<tr>
<th>File Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach (FileInfo file in Model)
{
<tr>
<td>
<input type="checkbox" name="Name" value="@file.Name" />
@file.Name
</td>
</tr>
}
</tbody>
</table>
</div>
<input type="submit" id="Held" name="Held file" value="View" />
<input type="submit" id="Release" name="release" value="Release" />
}
為了避免混淆,“ 查看”按鈕重定向到ViewFile方法,而“ 發布”按鈕重定向到Releasefile方法。
您有多種選擇可以做到這一點。
您可以劫持“提交”按鈕單擊並基於單擊的按鈕更新表單操作屬性值,然后使用javascript進行表單提交。
您可以將網址保留為按鈕上html5數據屬性中的2個操作方法。
<input type="submit" data-myaction="@Url.Action("View")" value="View"/>
<input type="submit" data-myaction="@Url.Action("Release")" value="Release"/>
使用Url.Action
方法生成到action方法的正確相對路徑是一種安全的做法。 讓該方法擔心為您生成正確的路徑。
和JavaScript
$(function () {
$("input[data-myaction]").click(function(e) {
e.preventDefault(); // stop the normal form submit
// read the data attribute and update the forms action and do a submit
$(this).closest("form").attr('action', $(this).data('myaction')).submit();
});
});
另一個選擇是使用html5 formaction
,它不需要任何JavaScript劫持。 指定formaction
屬性值時,它將覆蓋父表單的action屬性。 如果您有多個提交按鈕,並且要提交兩種不同的操作方法,那么此功能非常有用(您的用例)
<input type="submit" formaction="@Url.Action("View")" value="View"/>
<input type="submit" formaction="@Url.Action("Release")" value="Release"/>
1-HTML5 formaction和formmethod屬性
<input type="submit" name="view" value="view" formaction="ViewFile" formmethod="post" />
<input type="submit" name="Release" value="Release" formaction="ReleaseFile" formmethod="post" />
2-jQuery / JavaScript代碼
$(document).ready(function () {
$("#Held").click(function () {
$("form").attr("action", "/HoldFiles/ViewFile");
});
$("#Release").click(function () {
$("form").attr("action", "/HoldFiles/ReleaseFile");
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.