MVC 5 Calling C# function from button in view [duplicate] - c#

This question already has answers here:
HTML button calling an MVC Controller and Action method
(19 answers)
Closed 5 years ago.
Looking for a way to call a c# function with a button inside one of my views? This seems like something trivial, but I haven't been able to find a solution for this. Any help is appreciated!
This is kinda how I expected it to be implemented, that or a #Html.Button
<input type="button" value="Sign Out" onclick="#SignOut()"/>

There is no simple way of dynamically executing server function inside the Razor views.
You can however using button redirect the user to another route that will execute the server function.
Using the #Html.ActionLink("your text", "actionName", "controllerName") Razor helper.
Doing so you will create an element that on click will redirect the user to the specified route.

Related

How to dynamically add/delete rows in asp.net mvc? [duplicate]

This question already has an answer here:
Submit same Partial View called multiple times data to controller?
(1 answer)
Closed 6 years ago.
I want to have an "add row" functionality in my asp.net mvc View.
One way I can do it is have n hidden rows and unhide a row each time "add row" is clicked. But then how would I handle serial numbers (each row would have a serial number) when a row is deleted.
I don't want to do it with JS. What would be the best approach. Should I do it from code behind? Any suggestions?
I am not sure why you do not want to do it with javascript and everything on the client side. It would be a better experience for the user and it would be quicker.
However, since you specifically indicated you want to do it in code behind then do this. Create a partial view and put the required html for a new row into it. Then you need to call your controller to serve that html to you from the client side. You can do that using AJAX. Here is how with jQuery:
$.ajax({
type: "GET",
url: "/Home/GetSomePartialView/",
data: someArguments,
success: function (viewHTML) {
$("#someDiv").html(viewHTML);
},
error: function (errorData) { onError(errorData); }
});
The above will get the html and inject it into an element with ID someDiv.
You will need an action in controller to serve the html. Here is some code:
public Action result GetSomePartialView(SomeArgumentModel someArguments)
{
return PartialView("_NewRow");
}
Call a webmethod using Ajax that calls the delete SQL for you and rebinds the grid with the new results in JS.

ASP.NET MVC post to controller action from same controller

I'm working on this project that currently has the follow method:
[HttpPost]
public ActionResult Service(string identifier)
This function is currently being used by a webpage form
<form method="POST" action="/Connector/Service">
<input type="hidden" id="identifier" name="identifier" value="#TempData["Identifier"]"/>
So Service is used when the user clicks on a button that submits the form on the webpage. The user is taken to this page from another action.
I have to implement a feature where sometimes instead of taking the user to the webpage, the user goes directly to the Service method from the action.
I found this thread when searching: ASP.NET MVC: RedirectToAction with parameters to POST Action
But it seems like that is bad design and returning RedirectToAction with Service actually did not work for me.
return RedirectToAction("Service", new {identifier})
Upon more search it seems like I actually cannot make a post request from my controller. Asp.NET MVC : redirect to another controller with POST Action
Any ideas on what I could do here? I am fairly new to ASP.NET and have no idea what to do at this point. All help is appreciated, thanks.
So turns out I could just call the function directly by doing
Service(identifier)
I kept thinking I had to use RedirectToAction so that didn't even cross my mind.
Thanks Sherlock for the answer!
The tag [post] is used only when a form is submited by the view itself, if your page redirect to a controler function from another view than the one your in, you have two option:
Use the [get] tag, you will probably have more luck with this tag, although i don't know exactly how it work (this might be edited)
Use the tagless function with parameters, if you rediret from another view to a function with a parameter, like public ActionResult Service(string identifier) that has no tag, you will automatically reach this function.
if the parameter haven't been specified, it will simply be NULL.

how to remove the extension .aspx of first login page on my web application [duplicate]

This question already has answers here:
Remove HTML or ASPX Extension
(7 answers)
Closed 9 years ago.
hi coder i have an application whos default page is login page when every i run my web application localhost:49194/InventoryTool/Login.aspx page will appear.
Now what i want is to change that localhost:49194/InventoryTool/Login.aspx page to localhost:49194/InventoryTool/Login i made some changes in Application_BeginRequest to make that change but its doesn't work
I make changes in global.asax
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RoutingData(RouteTable.Routes);
}
private void RoutingData(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("", "Login/", "~/Login.aspx");
routeCollection.MapPageRoute("", "Details/", "~/Details.aspx");
routeCollection.MapPageRoute("", "Reports/", "~/Report.aspx");
routeCollection.MapPageRoute("", "Error/", "~/Error.aspx");
}
by doing this when i go to Login.aspx page it wont change but when i go to details.aspx page it show me Details only
Now can you suggest any thing to me?
Can you give me some ideas how to remove .aspx extension from my login page or can you give me some sample code for that?
You need to implement routing or url rewriting to achieve an effect like this.

Things that are similar to C# event handlers in PHP [duplicate]

This question already has answers here:
Events on PHP. Is it possible?
(6 answers)
Closed 9 years ago.
I'd like to know what is the thing similar to C# event handlers in PHP.
e.g. When a button is clicked, every functions attached to the event on click are called.
There is nothing like this out of the box in PHP, but there is a framework called PRADO which tries to bring event-driven development to PHP.
In PHP you make use of isset to see if a button was clicked. For instance:
if (isset($_POST['button'])) {
$Variable = $_POST['button'];
}
The button then looks like this in HTML
<input type="submit" name="button" value="ButtonName">
In web development this is managed by client side. So it's not PHP which check user action, it's javascript and HTML.
you can bind HTML to PHP action with HTML tags <form> and <a>.
For pure JavaScript events, you can start to check this MDN documentation
There is lots of JS libraries built to ease event management like jQuery.
You can either use Javascript events triggered by the html tag onclick. Have a look at this if you choose to use that way.
<button onclick="myFunction()">Click me</button>
You can alo use jQuery to trigger various events for example. Have a look at this.
If you choose to use this solution you can also trigger various ajax based functions.
$('button').click()
But if you want to trigger serversided events you need to add a form and submit the values to a php script for example:
<?php
if(isset($_POST['someVal'])){
echo "Hello";
}
?>
<form action="" method="post">
<input type="text" name="someVal" />
<input type="submit" value="send" />
</form>

Removing a specific query from url using jquery [duplicate]

This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 9 years ago.
Good morning guys, i have encountered a problem here.
This is my current URL:
blabla/shoe?type=boot&zoom=true
i want to remove the "&zoom=true" part, without reloading the page.
But bare in mind that the URL may also look like this:
blabla/shoe?zoom=true
how do i accomplish this, can someone give me an example of Jquery statements. So can start from there. thank u in advance
The reason why i wanna do this is because of the following:
<div class="standard" style="<%=isZoom?"":"display:none;" %>">
isZoom() is checking for any zoom query in the URL, i want the &zoom=true to go away so that the div can be shown
Changing the URL in the browser will reload the page. What's more, the loaded page will not contain your ASP.NET condition. It will only say style="display: none;", so even if you could change the URL without reloading the page, you'd still need to reload the page to have ASP.NET render something else.
The good news, though, is that you can show a div without changing the URL or reloading the page:
$('.standard').show();

Categories