how to call method in button onclick event in MVC 4? - asp.net-mvc

I am developing a MVC application.
In Edit View, I want to have save/submit button and cancel button.
By default MVC given 'back to list' link.this link calls index() method.
Now, I want to call this index() method on Cancel button's Onclick event , how to do this ?
<input type="submit" value="Save" onclick="return validate();" id="btnSubmit" />
<input type="Button" value ="Cancel" onclick="Index()"style=" font-size:"1.2em"/>
What changes I have to make in above code?

You could use the window.location.href to redirect to a specified url:
<input type="Button" value="Cancel" onclick="window.location.href='#Url.Action("index")'" style=" font-size:"1.2em"/>
or simply use an anchor instead of a button:
#Html.ActionLink("Cancel", "Index")

Related

asp-action and asp-controller executes post method after reedirects

I use from asp-action in order to logout action as:
<form asp-action="Logoff" asp-controller="Account">
<button type="submit" class="btn-link dropdown-item">
<i class="mdi mdi-logout font-size-16 align-middle me-1"></i>
Logout
</button>
</form>
So I want to use the same to redirect to a new view, that view has a inside it and the controller has a HttpPost, so when I click on it executes the post method, is there a way to avoid the execution of HttpPost method and still using asp-actions?
I change the button type to button, but it do anything when it's clicked
You can try to add method="get" into the form,so that when you click on it executes the get request:
<form method="get" asp-action="Logoff" asp-controller="Account">
<button type="submit" class="btn-link dropdown-item">
<i class="mdi mdi-logout font-size-16 align-middle me-1"></i>
Logout
</button>
</form>

Input parameter of action is null when using asp-route-{value}

I want to download a file that its name has been saved in my database. I use the following code to download the file:
public async Task<IActionResult> DownloadPm(string fileName)
{
.
.
.
}
And use the following code in view:
<form method="get">
<button type="submit" class="btn btn-success btn-table" asp-controller="Page" asp-
action="DownloadPm" asp-route-fileName="#item.mainFileName">ِDownload</button>
</form>
The problem is that the input parameter in DownloadPm action is null.
You are using tag helper with a button which actually does nothing but triggers the html form. To keep your current code you can have a hidden field in the form which will post the value to the action. Sample code:
<form method="get">
<input name="fileName" type="hidden" value="#item.mainFileName">
<button type="submit" class="btn btn-success btn-table" asp-controller="Page"
asp-action="DownloadPm">ِDownload</button>
</form>
Better approach: Since you are using Http GET and there is no other control in the form then why bother with Form? Instead go with an anchor element and style it like a button. Like the followings:
<a class="btn btn-success btn-table" href="/Page/DownloadPm?fileName="#item.mainFileName">Download</a>

How to access to the new page with form and target="_blank" through capybara

I am trying to scrape this page with capybara + poltergeist.
<form action="/agency/advertiser/login" method="POST" role="form" target="_blank">
<input type="hidden" name="csrfToken" value="token"/>
<input type="hidden" name="account_id" value="id">
<input type="submit" value="login" class="btn btn-warning">
</form>
I could access to the element below, and tried click.
<input type="submit" value="login" class="btn btn-warning">
However, I can't access to the new page that opens after the click. How can I do that?
You need to tell Capybara you want to work in the new window. You do that by getting a handle to newly opened window and then using within_window or switch_window.
new_window = page.window_opened_by do
# perform whatever action causes the new window to open
page.find('form').click_button
end
page.within_window(new_window) do
# perform actions in the new window
end
# context returned to the original window
...

Input button in MVC asp.net

How to use Input button for #Html.ActionLink?
In my cshtml I have this code:
<div>
#Html.ActionLink("Back to menu", "HomeController")
</div>
Now I whant to use instead hyperlink button (Back to menu), input submit button, so this button:
<input class="btn" type="submit" value="Back to menu">
Thanks.
You can't - you can use css so the link will look like button, or you can utilize Url.Action and javascript instead. I.E.:
<input class="btn" type="submit" value="Back to menu" onclick="window.location='#Url.Action("Index", "Home")';return false" />
Ok, I solved it, Ondrej Svejdar thanks for advice, I use javascript and works fine:
<input class="someCss" type="button" value="Back to Edit Menu" onclick="Redirect()"/>
And under button I use this javascript:
<script type="text/javascript">
function Redirect() {
var url = "#Url.Action("Registration")";
location.href = url;
}
</script>
So result is:
<input type="button" value="Button" />
value="button" />

Wrong button executes the method="post"

How do I prevent "button1" from executing the emailform.php action?
I have three buttons on my page; two of which are supposed to just make sure the text boxes are not blank and the third one is a "submit" button that is supposed to email me the form results via a emailform.php action. The problem is that when I click on button 1 to validate the text boxes, it does that correctly but then tries to email the form.
<form name="surveyform" action="/emailform.php" method="post">
<button id="button1" onclick="return validate_question1()">Go to question 2</button>
<button id="button2" onclick="return validate_question2()">Go to question 3</button>
<input name="submit" value="submit" class="submit" type="submit" />
Try this -- set the type:
<button type ="button" id="button1" onclick="return validate_question1()">
Might need to see the onclick function.

Resources