开发者

webform with one text box and two buttons

开发者 https://www.devze.com 2023-03-20 16:31 出处:网络
How can I have two buttons posting to different actions for the same text box? for eg: I have two buttons \"Get Student(xls)\" and \"Get Student(pdf)\" and I want to trigger different ActioResult met

How can I have two buttons posting to different actions for the same text box?

for eg: I have two buttons "Get Student(xls)" and "Get Student(pdf)" and I want to trigger different ActioResult methods. I am only able to trigger one method for launching excel, which is: "/Student/StudentExcel/" The second method for launching pdf is: "/Student/StudentPdf/".

Here is my screenshot for the form:

webform with one text box and two buttons

Here is my code for the view page:开发者_如何学运维

<%@ Page language="c#" AutoEventWireup="true" %>
<%@ Import NameSpace="System.IO" %>
<%@ Import NameSpace="System" %>
<%@ Import NameSpace="System.Data" %>
<%@ Import NameSpace="System.Web" %>
<%@ Import NameSpace="System.Text" %>
<%@ Import Namespace="STUDENT.Controllers" %>


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Get Student File</title>
</head>
<body>

    <div>

    <form method="post" action="/Student/StudentExcel/">
    <label for="id">Student Number: </label>
    <input type="text" name="id" value="" />
    <br /><br />
    <input type="submit" value="GetStudent(xls)"/>  &nbsp;&nbsp
    <input type="submit" value="Get Student(pdf)" /> 
    </form>
    </div>


How can I have two buttons posting to different actions for the same text box?

You can't do this without javascript and plain HTML.

Let me suggest you an alternative: you could use two submit buttons with different names that submit to the same controller action and inside this controller action you could retrieve the name of the submit button that was clicked and act accordingly.

<form method="post" action="/Student/Dispatch/">
    <label for="id">Student Number: </label>
    <input type="text" name="id" value="" />
    <br /><br />

    <input type="submit" value="Get Student(xls)" name="xls" />  
    &nbsp;&nbsp
    <input type="submit" value="Get Student(pdf)" name="pdf" /> 
</form>

and inside the Dispatch action:

[HttpPost]
public ActionResult Dispatch(string pdf, string id)
{
    if (!string.IsNullOrEmpty(pdf))
    {
        // The GetPdf submit button was clicked
        return StudentPdf(id);
    } 

    // The GetXls submit button was clicked
    return StudentExcel(id);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号