我有一个显示文件名列表的视图。我还有两个名为查看和发布的按钮。当我从列表中选择文件名并单击视图时,它会导航到相应的操作方法以及选择作为参数的文件名,并根据需要执行功能。
但是当我在选择文件名后单击 Release 时,它会导航到相应的操作方法,但不会将文件名作为参数传递给action方法。它显示为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 方法 并且 Release 按钮重定向到 Releasefile 方法。
答案 0 :(得分:2)
您有多种选择可以执行此操作。
您可以劫持提交按钮单击并根据单击的按钮更新表单操作属性值,并使用javascript进行表单提交。
您可以将url保留在按钮上的html5数据属性中的2个操作方法中。
f
使用<input type="submit" data-myaction="@Url.Action("View")" value="View"/>
<input type="submit" data-myaction="@Url.Action("Release")" value="Release"/>
方法生成操作方法的正确相对路径是一种安全的做法。 让方法担心为您生成正确的路径。
和javascript
Url.Action
另一个选项正在使用html5 formaction
,它不需要任何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();
});
});
属性值时,它将覆盖父窗体的action属性。如果您有多个提交按钮,并且有两种不同的操作方法可以提交到(您的用例)
formaction
答案 1 :(得分:1)
1-HTML5格式和formmethod属性
from pyspark.sql.types import DoubleType
new_df = dataframe.withColumn('gen_val', dataframe['gen_val'].cast('double'))
2-jQuery / JavaScript代码
<input type="submit" name="view" value="view" formaction="ViewFile" formmethod="post" />
<input type="submit" name="Release" value="Release" formaction="ReleaseFile" formmethod="post" />