Nirmalya Ghosh
Nirmalya Ghosh

Reputation: 2500

Get button id of a form in cshtml

I've two forms on the same page. On clicking any one of the buttons, both queries of the buttons are being submitted as I've used the function IsPost like if(IsPost){ //run queries}

Since both the forms are on the same page, on submission of any button, both the events of the two buttons are triggered.

I want to run separate queries on submission of each of the buttons.

In php, we used if(isset($_REQUEST['buttonname'])){ //run queries }.

Is there any such way, I can achieve the same in cshtml?

Update

What if I already have passed a value in the buttons?

Like for eg:

<div class="btn-group pull-right"> @{ var followersearchcommand = "SELECT * from follow where follow_followerid = @0 and follow_followingid = @1"; var followsearch = db.Query(followersearchcommand, follower_id, row.student_id); } @if(followsearch.Count > 0){ <button class="btn btn-primary" type="submit" name="unfollow_followingid" value="@row.student_id" title="Follow" style="display: none"></button> <button class="btn btn-primary" type="submit" name="unfollow_button" value="unfollow" title="Follow">UnFollow</button> } else{ <button class="btn btn-primary" type="submit" name="followingid" value="@row.student_id" title="Follow" style="display: none"></button> <button class="btn btn-primary" type="submit" name="follow_button" value="follow" title="Follow">Follow</button> } </div>

Upvotes: 0

Views: 2227

Answers (2)

GmG
GmG

Reputation: 1372

Assign to both buttons the same name and test their value:

@{
    if (IsPost){
        if(Request["button"] == "buttonA"){
            // button A is pressed
        } else {
            // button B is pressed
        }
    }
}

<form method="post">
    <input type="submit" name="button" value="buttonA" />
    <input type="submit" name="button" value="buttonB" />
</form>

Edited

If you want to have two distinct forms, you could use the IsEmpty() method:

@{
    if (IsPost){
        if(!Request["buttonA"].IsEmpty()){
            // button A is pressed
        } else {
            // button B is pressed
        }
    }
}

<form method="post">
    <input type="submit" name="buttonA" value="buttonA" />
</form>
<form method="post">
    <input type="submit" name="buttonB" value="buttonB" />
</form>

Upvotes: 2

Antoine Cloutier
Antoine Cloutier

Reputation: 1330

html:

<form id="a">
    <button type="submit">Submit A</button>
</form>

<form id="b">
    <button type="submit">Submit B</button>
</form>

javascript:

$(function(){
    $("#a").submit(function(e){
        e.preventDefault();
        // Query A
        alert('A');
    });

    $("#b").submit(function(e){
        e.preventDefault();
        // Query B
        alert('B');
    });
});

Demo

Edit:

You can make an ajax call to your controller's action:

$.ajax({
    type: "POST",
    url: "@Url.Action("actionName", "controllerName")",
    data: { /* your data */ },
    success: function (data, textStatus, jqXHR) {
        // success
    },
    error: function (jqXHR, textStatus, errorThrown) {
        // error
    }
});

And in your controller:

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult actionName(/* your data */)
{
  var success = DoSomething();

  if (!success)
  {
    throw new HttpException(500, "Server error!");
  }

  return Json("");
}

Something along those lines...

Hope this helps!

Upvotes: 0

Related Questions