ASP.NET MVC Using Two Inputs Using Html.BeginForm

Is it possible to have two inputs or control buttons for two different actions inside the same controller related to Html.BeginForm?

In the following example, I want the Subscribe tab to trigger the Subscribe action on the controller. It works well. I want the Unsubscribe entry to trigger the Unsubscribe action. How to do it? 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 shown below:

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

I was hoping I could trigger every action from the same Form element, but this seems to be wrong. How do I save an MVC pattern, but if possible, avoid scripting? Published HTML is part of the partial view displayed 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> <% } %> 
+4
source share
3 answers

The HTML form element has only one action . Therefore, if your buttons do not change that with JavaScript, each form receives only one URI. Do you want your site to work only with JavaScript enabled?

Another option is to give the buttons a name , in which case you will get value as part of the placed form, for example:

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

Then you can perform one action that checks the value, for example:

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

Since newsletter is the key in the hosted form, MVC will bind its value to the action argument.

+8
source

Or using RedirectToAction

  <%using(Html.BeginForm("RedirectingAction", "NewsLetter")){ %> <input type="submit" class="submit" name="newsletter" value="Subscribe" /> <input type="submit" class="submit" name="newsletter" value="Unsubscribe" /> <%} %> 

and in the controller:

  public ActionResult RedirectingAction() { TempData["Form"] = Request.Form; return RedirectToAction(Request.Form["newsletter"].ToString()); } public ActionResult Subscribe() { // Do stuff return View(); } public ActionResult Unsubscribe() { //Do something else return View(); } 
+1
source

You can accomplish this with jquery:

 function Subscribe() { $.post(urlToSubscribeAction, $('#formId').serialize(), function () {// Do something after post}); return false; } function UnSubscribe() { $.post(urlToUnsubscribe, $('#formId').serialize(), function() { // Do something after post}); return false; } 

Add an onclick handler to the submit buttons that will call your javascript functions, and then make the form post in the correct url. Be sure to replace formId with the corect identifier of your form element.

 <input type="submit" class="submit" value="Subscribe" onclick="Subscribe()" /> <input type="submit" class="submit" value="Unsubscribe" onclick="Unsubscribe()" /> 
0
source

Source: https://habr.com/ru/post/1339125/


All Articles