Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, December 11, 2009 7:39 AM
Hello,
I need to have a form on every view of a web site to subscribe and unscubscribe a newsletter.
I created a partial view and three actions:
Subscribe (GET) - Returns the form.
Subscribe (POST) - Adds email to database.
Unsubscribe (POST) - Removes email from database.
My problem is how to have the two buttons subscribe and unsubscribe in the form to link to the two actions and to be able to submit the form without refreshing the page.
How should I do this?
If you think I should change the actions or anything else please let me know.
Thank you,
Miguel
All replies (6)
Friday, December 11, 2009 8:38 PM ✅Answered
@ shapper
Why not use multiple submit buttons?:
(without JavaScript) ~~ same name for both buttons.
on your .aspx page, add a second button:
<input type="submit" value="First" name="submitButton"/>
<input type="submit" value="Second" name="submitButton"/>
in your Controller:
// POST: /Home/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Movie movieToEdit, string submitButton)
{
if (submitButton == "First") *et cetera
*
The above lets you have one controller perform multiple tasks with just one form.
Regards,
Gerry (Lowry)
Friday, December 11, 2009 11:09 PM ✅Answered
There is a good solution here .
hope this helps.
Friday, December 11, 2009 7:44 AM
you can use Ajax for this solutions.
Friday, December 11, 2009 2:39 PM
@virender is correct. If you want to submit a form without refereshing the page you have to use an Ajax solution.
Asp.net mvc makes submitting forms through ajax really easy. If you're using the Html.BeginForm syntax, you can easily modify that to Ajax.BeginForm and pass in a few extra parameters.
Take a look at the following tutorials on using Ajax with asp.net mvc
http://davidhayden.com/blog/dave/archive/2009/05/19/ASPNETMVCAjaxBeginForm.aspx
http://nerddinnerbook.s3.amazonaws.com/Part10.htm
You an also use Jquery for doing ajax . Here are a few Ajax examples:
http://docs.jquery.com/Ajax
Friday, December 11, 2009 11:17 PM
Hi Miguel,
You can use AJAX. as follows put your all page in Update Panel by taking ScriptManage in the page
as follows:
In body tag you can put following code.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
// your form with submit button
</ContentTemplate>
</asp:UpdatePanel>
I hope this may help u.
Friday, December 11, 2009 11:25 PM
Hi Miguel,
You can use AJAX. as follows put your all page in Update Panel by taking ScriptManage in the page
as follows:
view plaincopy to clipboardprint?
- In body tag you can put following code.
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- </asp:ScriptManager>
- <asp:UpdatePanel ID="UpdatePanel1" runat="server">
- <ContentTemplate>
- // your form with submit button
- </ContentTemplate>
- </asp:UpdatePanel>
In body tag you can put following code.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
// your form with submit button
</ContentTemplate>
</asp:UpdatePanel>
I hope this may help u.
I think you still live in web form world ...
we are talking about MVC here