在单击两个不同的按钮时将相同的参数传递给操作 [英] Passing same parameter to an action on clicking two different buttons

查看:42
本文介绍了在单击两个不同的按钮时将相同的参数传递给操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示文件名列表的视图.我也有两个按钮,分别是 View Release .当我从列表中选择一个文件名并单击视图时,它会导航到适当的操作方法以及作为参数选择的文件名,并根据需要执行功能.

I have a View which displays a list of file names. Also i have two buttons called View and Release. When I select a file name from the list and click on view, it navigates to the appropriate action method along with the file name selected as a parameter and performs the functionality as required.

但是当我选择文件名后单击 Release 时,它会导航到适当的操作方法,但没有将文件名作为参数传递给操作方法.它显示为 null .

But when i click on Release after selecting a file name, it does navigates to the appropriate action method, but does not passes the file name as a parameter to the action method. It shows as null.

请注意, View Release 指向具有不同操作方法的单个控制器.

Please note that View and Release directs to a single controller having different action methods.

单击发布时,如何获取文件名作为参数?

How can i get to pass the filename as a parameter when i click on release?

请参见下面的代码:

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" />
}

为避免混淆, View 按钮重定向到 ViewFile 方法然后 Release 按钮重定向到 Releasefile 方法.

Just to avoid confusion, the View button redirects to the ViewFile method and the Release button redirects to Releasefile method.

推荐答案

1-HTML5格式和格式方法属性

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

这篇关于在单击两个不同的按钮时将相同的参数传递给操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆