Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Using two inputs with Html.BeginForm

Tags:

asp.net-mvc

Is it possible to have two inputs, or button controls, trigger two different actions within the same controller bound to Html.BeginForm?

In the following example, I want the Subscribe input to call Subscribe action on the controller. This works well. I want the Unsubscribe input to call my Unsubscribe action. How do I make this happen? TIA.

<% using (Html.BeginForm("Subscribe", "NewsLetter"))
    {%>    
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.EmailAdress) %>
            <%= Html.ValidationMessageFor(
                model => model.EmailAdress) %>                                
        </div>                        
        <div >
            <input type="submit" class="submit" value="Subscribe" />
            <input type="submit" class="submit" value="Unsubscribe" />
        </div>         
<% } %>

Update:

Just more context to this question:

My NewsLetterController has two actions Subscribe and Unsubscribe as posted below:

public class NewsLetterController : Controller
    {      
        [HttpPost]
        public ActionResult Subscribe(NewsletterSubscriber subscriber)
        {
           // do something

        }

        [HttpPost]
        public ActionResult Unsubscribe(NewsletterSubscriber subscriber)
        {
            // do something
        }

I was hoping that I can invoke each action from the same Form element, but this apparently is false. How do I still maintain the MVC pattern but avoid scripting if possible? The posted HTML is part of a partial view rendered on the main page.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl
                                   <AkwiMemorial.Models.NewsletterSubscriber>" %>

   <h2><strong>Newsletter</strong></h2>
    <legend>Enter your email</legend>

    <% using (Html.BeginForm("Subscribe", "NewsLetter"))
       {%>    
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.EmailAdress) %>
                <%= Html.ValidationMessageFor(model => model.EmailAdress) %>
            </div>                        
           <div >
             <input type="submit" class="submit" name="sub" 
                    value="Subscribe" />
              <input type="submit" class="submit" name="unsub" 
                     value="Unsubscribe" />
            </div>         
    <% } %>
like image 485
Klaus Nji Avatar asked Jan 21 '23 13:01

Klaus Nji


1 Answers

An HTML form element has only one action. So unless your buttons change that with JavaScript, each form gets only one URI. Do you want your site to work with JavaScript enabled only?

Another option is to give the buttons a name, in which case you'll get the value as part of the posted form, e.g.:

<input type="submit" class="submit" name="newsletter" value="Subscribe" />
<input type="submit" class="submit" name="newsletter" value="Unsubscribe />

Then you can have a single action which checks the value, e.g.:

public ActionResult Subscribe(string newsletter)
{
    if ("subscribe".Equals(newsletter, StringComparison.OrdinalIgnoreCase))
    {
        //...

Because newsletter is a key in the posted form, MVC will bind its value to the action's argument.

like image 96
Craig Stuntz Avatar answered Jan 30 '23 21:01

Craig Stuntz