vinothkumarsi, I think something like this might work (I have not tried it):
using the movie database tutorial as an example: Listing 5 – Views\Home\Create.aspx
<p>
<input type="submit" value="Create" />
</p>
try to modify the above something like:
<p>
<input type="submit" value="Create" />
<input type="submit" value="CreateClassicMovie" />
</p>
Listing 4 – Controllers\HomeController.cs (modified Create method)
add something like:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateClassicMovie([Bind(Exclude = "Id")] Movie movieToCreate)
{
// special processing for classic movies
TIMTOWTDI, there is more than one way to do it.
Let's us know if this works.
Even if it does, there are likely better methods.
Regards,
Gerry (Lowry)
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
You can do that, but you will need to write some JavaScript to handle this. Simply adding a new <intput type="submit"> element and changing it's value attribute won't help because it will POST the form to the same action method. If you do use this approach,
you will also need to ensure that the action methods are distinct so that you don't get an error indicating that an ambiguous action method was found.
You could try something like the following (Let's assume this is in Index.aspx)
I realize I did not fully think this through ~~ I'm guesing
that it's simpler that I thought and that JavaScript is not necessary.
Since both buttons are in the same form,
the browser will indicate which button was clicked, n'est-ce pas?
The <% using (Html.BeginForm()) {%>
.
.
.
<% } %>
will send everything back to
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")] Movie movieToCreate)
and vinothkumarsi need only decide whether the
<input type="submit" value="Create" />
or
<input type="submit" value="CreateClassicMovie" />
was clicked.
That way, one need not worry about JavaScript being enabled.
Regards,
Gerry (Lowry)
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Yes, that can also be done. If you include a name attribute in your <input> element, for example <input type="submit" name="X" value="submit"/> then Request.Form will contain an entry that indicates the name of the button that was clicked
Jacques, yes ~~ I forgot the name='somename'
part.
QUESTIONS:
(a) is name='somename' absolutely necessary?
i.e., without name='somename' is there
something the equivalent of an anonymous name
that can be referenced for
<input type="submit" value="Create" />
<input type="submit" value="CreateClassicMovie" />
back at
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")] Movie movieToCreate)
???? [if yes, what is the correct syntax???]
(b) for
<input type="submit" name="normalCreate" value="Create" />
<input type="submit" name="classicCreate" value="CreateClassicMovie" />
back at
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")] Movie movieToCreate) what is the correct syntax to determine which "create" was chosen?
I'm trying to figure out the syntax but I'm at a loss on how to proceed.
Request.Form["classicCreate"] seems inadequate because
somehow "Request.Form" needs to know which form object
it needs to interrogate. Also, does Post return a string, char, bool?
I've not tried var mySubmit = Request.Form["normalCreate"];
I've hunted via Google and MSDN and can only find very thin
discussions by ScottGu plus comments to one of his blogs;
nothing seems to have been mentioned since around CTP2.
Also, just curious, does <input type="reset" value="Reset" /> return to the server (i.e., must it be handled by code) or
is it taken care of 100% by the browser?
Thank you.
Regards ~~ Gerry (Lowry)
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
public ActionResult Create(...., string submitButton) {
}
then the submitButton will either contain "First" or "Second", depending on which one was clicked.
As for your question about the reset input type. No, to my knowledge, all that happens is that the browser resets all form fields to their initial values. It's handled by the browser.
@ vinothkumarsi ~~ I've used the syntax suggested by Jacques
and it works wonderfully.
issue summary: (without JavaScript)
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
------------------------------------------------------------------------------------ issue summary: (with JavaScript) ~~ see Jacques first post in *this* thread.
@ Jacques, thank you for your help;
the non JavaScript solution is quite simple
once one gets the syntax correct ~~ just
curious, if you had not been here to help,
how would I have figured out the syntax?
I could find nothing anywhere, probably
because I did not know where to look.
I'm glad vinothkumarsi asked this question because I need this technique also.
Thank you both,
Gerry (Lowry)
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
vinothkumars...
Member
36 Points
36 Posts
submit Button In Mvc
Apr 27, 2009 08:41 AM|LINK
"ASP.NET MVC"
gerrylowry
All-Star
20515 Points
5713 Posts
Re: submit Button In Mvc
Apr 27, 2009 11:59 AM|LINK
vinothkumarsi, I think something like this might work (I have not tried it):
using the movie database tutorial as an example:
Listing 5 – Views\Home\Create.aspx
<p>
<input type="submit" value="Create" />
</p>
try to modify the above something like:
<p>
<input type="submit" value="Create" />
<input type="submit" value="CreateClassicMovie" />
</p>
Listing 4 – Controllers\HomeController.cs (modified Create method)
add something like:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateClassicMovie([Bind(Exclude = "Id")] Movie movieToCreate)
{
// special processing for classic movies
TIMTOWTDI, there is more than one way to do it.
Let's us know if this works.
Even if it does, there are likely better methods.
Regards,
Gerry (Lowry)
jeloff
Contributor
2493 Points
432 Posts
Microsoft
Re: submit Button In Mvc
Apr 27, 2009 05:28 PM|LINK
Hi
You can do that, but you will need to write some JavaScript to handle this. Simply adding a new <intput type="submit"> element and changing it's value attribute won't help because it will POST the form to the same action method. If you do use this approach, you will also need to ensure that the action methods are distinct so that you don't get an error indicating that an ambiguous action method was found.
You could try something like the following (Let's assume this is in Index.aspx)
<% using(Html.BeginForm("Submit", "Home")) { %>
<% =Html.TextBox("foo") %>
<% =Html.TextBox("bar") %>
<input type="submit" value="submit 1">
<intput type="submit" value="submit 2" onclick="submit2()">
<% } %>
In your controller you will need to different action methods, for example
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Submit(string foo) {
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AnotherSubmission(string foo, string bar) {
}
The 2nd submit button's onclick function using jQuery:
function submit2() {
$.post("/Home/AnotherSubmission", { foo: $("#foo").val(), bar: $("#bar").val() });
}
Jacques
gerrylowry
All-Star
20515 Points
5713 Posts
Re: submit Button In Mvc
Apr 27, 2009 06:04 PM|LINK
Hi Jacques,
I realize I did not fully think this through ~~ I'm guesing
that it's simpler that I thought and that JavaScript is not necessary.
Since both buttons are in the same form,
the browser will indicate which button was clicked, n'est-ce pas?
The <% using (Html.BeginForm()) {%>
.
.
.
<% } %>
will send everything back to
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")] Movie movieToCreate)
and vinothkumarsi need only decide whether the
<input type="submit" value="Create" />
or
<input type="submit" value="CreateClassicMovie" />
was clicked.
That way, one need not worry about JavaScript being enabled.
Regards,
Gerry (Lowry)
jeloff
Contributor
2493 Points
432 Posts
Microsoft
Re: submit Button In Mvc
Apr 27, 2009 06:18 PM|LINK
Hi Gerry
Yes, that can also be done. If you include a name attribute in your <input> element, for example <input type="submit" name="X" value="submit"/> then Request.Form will contain an entry that indicates the name of the button that was clicked
Jacques
gerrylowry
All-Star
20515 Points
5713 Posts
Re: submit Button In Mvc
Apr 27, 2009 07:52 PM|LINK
Jacques, yes ~~ I forgot the name='somename' part.
QUESTIONS:
(a) is name='somename' absolutely necessary?
i.e., without name='somename' is there
something the equivalent of an anonymous name
that can be referenced for
<input type="submit" value="Create" />
<input type="submit" value="CreateClassicMovie" />
back at
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")] Movie movieToCreate)
???? [if yes, what is the correct syntax???]
(b) for
<input type="submit" name="normalCreate" value="Create" />
<input type="submit" name="classicCreate" value="CreateClassicMovie" />
back at
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")] Movie movieToCreate)
what is the correct syntax to determine which "create" was chosen?
I'm trying to figure out the syntax but I'm at a loss on how to proceed.
Request.Form["classicCreate"] seems inadequate because
somehow "Request.Form" needs to know which form object
it needs to interrogate. Also, does Post return a string, char, bool?
I've not tried
var mySubmit = Request.Form["normalCreate"];
I've hunted via Google and MSDN and can only find very thin
discussions by ScottGu plus comments to one of his blogs;
nothing seems to have been mentioned since around CTP2.
Also, just curious, does <input type="reset" value="Reset" />
return to the server (i.e., must it be handled by code) or
is it taken care of 100% by the browser?
Thank you.
Regards ~~ Gerry (Lowry)
jeloff
Contributor
2493 Points
432 Posts
Microsoft
Re: submit Button In Mvc
Apr 27, 2009 08:17 PM|LINK
Hi Gerry
(a): I'm not sure.
(b): You can just rely on the default binding behavior in MVC. For example, if you have
<% using(Html.BeginForm()) { %>
....
<input type="Submit" Value="First" Name="submitButton"/>
<input type="Submit" Value="Second" Name="submitButton"/>
<% } %>
and an action method such as
public ActionResult Create(...., string submitButton) {
}
then the submitButton will either contain "First" or "Second", depending on which one was clicked.
As for your question about the reset input type. No, to my knowledge, all that happens is that the browser resets all form fields to their initial values. It's handled by the browser.
Jacques
gerrylowry
All-Star
20515 Points
5713 Posts
Re: submit Button In Mvc
Apr 28, 2009 12:53 AM|LINK
@ vinothkumarsi ~~ I've used the syntax suggested by Jacques
and it works wonderfully.
issue summary: (without JavaScript)
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
------------------------------------------------------------------------------------
issue summary: (with JavaScript) ~~ see Jacques first post in *this* thread.
@ Jacques, thank you for your help;
the non JavaScript solution is quite simple
once one gets the syntax correct ~~ just
curious, if you had not been here to help,
how would I have figured out the syntax?
I could find nothing anywhere, probably
because I did not know where to look.
I'm glad vinothkumarsi asked this question because I need this technique also.
Thank you both,
Gerry (Lowry)
paul.vencill
Contributor
6716 Points
1358 Posts
Re: submit Button In Mvc
Apr 28, 2009 05:21 AM|LINK
Gerry: http://www.google.com/search?source=ig&hl=en&rlz=1G1GGLQ_ENUS239&q=html+multiple+submit+buttons&aq=0&oq=html+multiple+sub
;)
vinothkumars...
Member
36 Points
36 Posts
Re: submit Button In Mvc
Apr 28, 2009 06:30 AM|LINK