ASP.NET MVC Default Button - asp.net-mvc

Can we use it like this...
<% using (Html.BeginForm("PerformSearch", "ClientSearch", FormMethod.Post, new { defaultbutton = "btnSearch" })) %>
Basically I want the enter key to raise the onClick event of btnSearch. This button is just a button, not a submit button.

One of the beauties of MVC in .NET is that it lets you easily write your own JavaScript helpers, which what you need to do in this case. Just write a simple function that binds the 'enter' key press to a click event on that button on pageload.

Related

how to use server side button click event in mvc4?

I have to make a textbox somewhere in the page and a button using ASPX View Engine.That page already contains partial views. User will enter his data (code) on textbox and at button click, server code will manipulate that data and show an alert or message that data is processed. How to do that in mvc4?
I have found that we can write server script in page like
<script runat="server">
Protected Sub Button1_Click(sender As Object, e As EventArgs)
ScriptManager.RegisterStartupScript(Me, [GetType](), "showalert", "alert('Message Sent');", True)
End Sub
Private Sub DataProcess(ByVal Data As String)
'Code to check data
End Sub
</script>
You can create a separate Ajax Form ccontaining only this button and the textbox. So you will call an Action in a certain controller then this Action will return a string or a Json. And via an UpdateTargetId property you can display a dialog box. Giving you the code is difficult because I don't know the structure of your code. You can aslo write the Ajax call using Jquery.
This link can help you

MVC call controller from radio button onclick

How can I call an action in my controller and pass a parameter from a radio button onclick event? I'm new to MVC....
I have this code which will call a javascript function, but I wanted to see if I can call an action and pass a parameter instead of calling a javascript function
#Html.RadioButtonFor(m => m.SiteAddressModel.SiteId, item.SiteId, new { #onclick = "radiobuttontest();"})
This is on of the differences between asp.net MVC and webforms in both cases you are using javascript to make a post or ajax call the difference is in webforms it's hidden by the framework. MVC give you greater control but it requires you do the work, if you have the javascript working your done.

how can I understand where this "submit" button/actionlink leads in ASP MVC 4?

I am new to ASP.NET MVC 4 and learning my way around an existing MVC 4 code base. I am trying to find the code that processes the the form corresponding to this submit button. I understand that the action link probably says how to process the "submit" button -- but I don't see any constructors that take three strings for an actionlink in the microsoft documentation.
I am confused because there is no action field in the input tag.
How do I find out what happens once the person hits submit?
#using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data", #class = "disableSubmit" }))
{
...
<div class="buttons">
<input type="submit" value="Upload" /> | #Html.ActionLink("Back to List", "Index", "Admin")
</div>
}
Update When I go to "view source" to see the raw HTML I see
<form action="/Lab/Upload" ...
So that means it goes to the lab/upload controller?
The Javascript for the disable submit looks like this:
// Disable submit button after click
$(function () {
$('.disableSubmit').submit(function () {
if ($(this).valid()) {
$('input[type="submit"]').attr('disabled', 'disabled');
}
});
});
If the submit button performs a regular form submit, then it will be inside of a <form> tag or #Html.BeginForm using block.
BeginForm will submit the form to the action that matches the name of the view, unless there is a parameter being passed that specifies the action name and/or controller name.
IF it is a form tag, then the action="something" attribute of the form tag will indicate the URL being submitted to, which is usually controllerName/ActionName` but could be different depending on what routing is setup.
The ActionLink you see is not related to the form or the submit, it is a regular link which is in effect a way for the user to go back to the previous page instead of submitting the form.
There is also the possibility that there is javascript attached to the submit button. That's harder to find unfortunately due to the many ways that javascript can be wired up to a button.
Edit: Based on your update, I would strongly suspect there's javascript that supports this form. I imagine the submit button is disabled until you meet some conditions that allow it to be displayed. Maybe permissions, maybe filling the form out completely, it's hard to say. Search the javascript for disableSubmit, as I suspect somewhere there is code that removes that class under certain conditions.
Edit 2: What is happening there is it disables the submit button after the first click so that you can't accidentally submit the for twice and cause problems with a duplicate submit(if this is Create form it avoids duplicate records). As far as I can tell there should be an action of the same name as the *.cshtml file that it submits to. Possibly with a [Post] or [HttpPost] attribute on the action.
Check whether the submit buttons in inside a form tag. If yes, what is the action attribute value of that ? That is the place the form will be submitted to.
You may see an HTML helper method called Html.Beginform in the view. This method render a form tag. You can go to the page and check the view source of the page to see what is the form tag looks like and what is the action method attribute value.
Ususally your controller will have an action method marked with HttpPost attribute to handle the form submit. Lets say your mark up is like this
<form action="User/EditUser">
<input type="text" name=Name" />
<input type="submit" />
</form>
Now in your UserController, there may be an action method like this
[HttpPost]
public ActionResult EditUser(SomeModelIfExist model)
{
// TO DO : save and redirect
}
the Html.ActionLink helper method renders an anchor tag. It has nothing to do with the form submit. So your action link helper will return the below markup
Back

how to implment click-once submit button in asp.net mvc 2?

I need to implement a click-once button for my asp.net mvc 2 application. I have just a very simple submit form, and when the user clicks on the submit button, I need to change its image to a "please wait..." type of graphics and disables the click event for further submits until the server comes back.
Is there an example or code snippet for this? I used to be able to do it in the asp.net webforms, but that technique does not apply here.
thanks!
To disable the submit button when you submit the form you can use the onSubmit function for the form and call a javascript function that will disable the button.
For example:
<% using (Html.BeginForm("Create", "Organisation", FormMethod.Post, new { autocomplete = "off", onsubmit = "disableSubmitButton()" }))
{ %>
//Your code
<input id="buttonName" type="submit" value="Create"/>
<% } %>
and then in your javascript section:
function disableSubmitButton() {
document.getElementById("buttonName").disabled = true;
};
Hope this helps.
Here is example of how create ajax login using asp.net mvc and jQuery:
How to implement a Client-side Ajax Login on Asp.Net MVC (A link to the solution for Asp.Net Webforms is in here)

How to send parameter in query string on ajax call in asp.net mvc

I want to send the selected page value on the querystring while navigating through paging.
The URL that is generated for paging is like this:
Link/Index?page=2
Link/Index?page=3
But on my URL it only shows Link/Index and performs the Ajax call. But if I disable my Javascript and then navigate through paging it gets Postback and has a URL like
Link/Index?page=2
Which is perfect. But I want this type of URL in an Ajax call as well.
How can I do this? Issue is if we navigate through pages when Javascript is enable it shows Link/Index and when user goes to page no 2 then 3 then 4 and press back button it goes to press page instead of page 3 then page 2.
Here is the code that generates the page links:
<%= Ajax.Pager(
new AjaxOptions {
UpdateTargetId = "divGrid", LoadingElementId = "divLoading"
},
ViewData.Model.PageSize,
ViewData.Model.PageNumber,
ViewData.Model.TotalItemCount,
new { controller = "LinkManagement", action = "Index" }
)%>
This isn't really an issue with the pager, but that's how ajax works. Because you haven't created a new full page request, nothing is stored in history to allow the back button to persist the ajax calls too. You need to use something like jquery.history (http://tkyk.github.com/jquery-history-plugin/) or jquery address api (http://www.asual.com/jquery/address/samples/api/#/section/?id=2.2).

Resources