I just cant seem to get this work. I have view that has a calculate button that calculates some things and displays it at textboxes on the view. Calculation terms come from other textboxes on the view. When i press the button it refreshes all the page, default
actions, but i need it to just refresh a specific part of the view.
Calculation terms come from other textboxes on the view
If the information you need to do the calculation is already on the screen, you should not use a submit button. Do the calculations and update the answer box in javascript function, and use a regular button to invoke the function.
Reporting by definition is different. Otherwise we would just show it on the screen.
You can handle this entirely through Javascript without even making any post backs.
Example Markup
<!-- Input boxes to store the values being calculated and the Answer -->
<input id='one' value='0' /> +
<input id='two' value='0' /> =
<input id='answer' />
<hr />
<!-- Button that will perform the calculation -->
<input type='button' onclick='Calculate()' value='Calculate' />
Example Javascript
<script type='text/javascript'>
function Calculate(){
var arg1 = parseFloat(document.getElementById('one').value);
var arg2 = parseFloat(document.getElementById('two').value);
document.getElementById('answer').value = arg1 + arg2;
}
</script>
Hello and thanks everyone for the reply, lets say that i dont want to make the calculations with javascript but through a method in my controller. Can this be done? I am asking because the javascript is visible to the web page and a user can see the calculations
that are done. They are not quantum calculations but i don't want them to be visible. Thanks again in
advance.
Yes, you can do it on server in a controller and action and then call the controller action with jquery/ajax and pass the required parameter to get the result from action method.
[HttpPost]
public ActionResult Calculate(string arg1, string arg2)
{
//Perform Calculation Here using arg1 and arg2 (You will need to parse them)
return yourCalculationResult;
}
That should at least get you going in the right direction.
i dont want to make the calculations with javascript but through a method in my controller. Can this be done? I am asking because the javascript is visible to the web page and a user can see the calculations that are done. They are not quantum calculations but
i don't want them to be visible.
yes, you can do using JQuery $.post or $.ajax function in below way.
$.ajax({
type: "POST",
url: '', data:'' success: function (data) {
},
error: function (data) {
}
});
Please remember to click “Mark as Answer” on the post that helps you.
This can be beneficial to other community members reading the thread.
DaltonGR
Member
1 Points
18 Posts
MVC3 multiple submit buttons
Jan 18, 2013 12:55 PM|LINK
Hello,
I just cant seem to get this work. I have view that has a calculate button that calculates some things and displays it at textboxes on the view. Calculation terms come from other textboxes on the view. When i press the button it refreshes all the page, default actions, but i need it to just refresh a specific part of the view.
ignatandrei
All-Star
137698 Points
22155 Posts
Moderator
MVP
Re: MVC3 multiple submit buttons
Jan 18, 2013 01:04 PM|LINK
Ajax. Please see http://bit.ly/mvc_ajax_jquery
ryanbesko
Contributor
3607 Points
630 Posts
Re: MVC3 multiple submit buttons
Jan 18, 2013 08:24 PM|LINK
If the information you need to do the calculation is already on the screen, you should not use a submit button. Do the calculations and update the answer box in javascript function, and use a regular button to invoke the function.
Rion William...
All-Star
32214 Points
5234 Posts
Re: MVC3 multiple submit buttons
Jan 18, 2013 08:35 PM|LINK
You can handle this entirely through Javascript without even making any post backs.
Example Markup
Example Javascript
<script type='text/javascript'> function Calculate(){ var arg1 = parseFloat(document.getElementById('one').value); var arg2 = parseFloat(document.getElementById('two').value); document.getElementById('answer').value = arg1 + arg2; } </script>Working Example
DaltonGR
Member
1 Points
18 Posts
Re: MVC3 multiple submit buttons
Jan 19, 2013 06:05 AM|LINK
Hello and thanks everyone for the reply, lets say that i dont want to make the calculations with javascript but through a method in my controller. Can this be done? I am asking because the javascript is visible to the web page and a user can see the calculations that are done. They are not quantum calculations
but i don't want them to be visible. Thanks again in
advance.
CPrakash82
All-Star
18722 Points
2900 Posts
Re: MVC3 multiple submit buttons
Jan 19, 2013 04:51 PM|LINK
Yes, you can do it on server in a controller and action and then call the controller action with jquery/ajax and pass the required parameter to get the result from action method.
Rion William...
All-Star
32214 Points
5234 Posts
Re: MVC3 multiple submit buttons
Jan 19, 2013 09:19 PM|LINK
Here's an example that will hit server-side.
Your View
@using (Html.BeginForm("Calculate", "Your Controller",FormMethod.Post)) { <input id='arg1' name='arg1' /> <input id='arg2' name='arg2' /> <input type='submit' value='Calculate' /> }Controller
[HttpPost] public ActionResult Calculate(string arg1, string arg2) { //Perform Calculation Here using arg1 and arg2 (You will need to parse them) return yourCalculationResult; }That should at least get you going in the right direction.
kumaru32
Member
48 Points
123 Posts
Re: MVC3 multiple submit buttons
Jan 20, 2013 07:46 AM|LINK
you can also make use of partial view for this problem. to refresh the particular section of a page.
jigar bagada...
Member
310 Points
51 Posts
Re: MVC3 multiple submit buttons
Jan 20, 2013 01:01 PM|LINK
yes, you can do using JQuery $.post or $.ajax function in below way.
$.ajax({ type: "POST", url: '',data:''
success: function (data) { }, error: function (data) { } });
This can be beneficial to other community members reading the thread.
With Regards,
Jigar Bagadai
ramramesh
Member
458 Points
158 Posts
Re: MVC3 multiple submit buttons
Jan 20, 2013 01:06 PM|LINK
Do you like this
http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx