-1

In my MVC application ,I have three button control on my view page.I am new to MVC.so dont know how to navigate through button control in MVC. I want to write three different methods on these three controls in controller. Like in ASP.net we had control event and we cn write code on these events . How we can achieve this in MVC.

Shalini Garg
  • 127
  • 1
  • 15
  • 5
    Noooo. ASP.NET MVC is very different to WebForms. Don't assume that things you did in WebForms will translate to ASP.NET MVC. Please go over some MVC tutorials and come back with specific problems that you run in to. – Rowan Freeman Aug 18 '14 at 05:38
  • 1
    You need to provide more information. what to the buttons do? Navigate to another page, return some value via AJAX? –  Aug 18 '14 at 05:41

2 Answers2

2

There are many ways depending on what exactly you are trying to achieve. The first is to use a standard hyperlink pointing to the corresponding controller action:

@Html.ActionLink("action 1", "actionName1", "controllerName")
@Html.ActionLink("action 2", "actionName2", "controllerName")
...

which would invoke the respective controller actions:

public ActionResult ActionName1()
{
    ...
}

public ActionResult ActionName2()
{
    ...
}

Another possibility is to use HTML forms:

@using (Html.BeginForm("actionName1", "controllerName"))
{
    <button type="submit">action 1</button>
}

@using (Html.BeginForm("actionName2", "controllerName"))
{
    <button type="submit">action 2</button>
}

...

A third approach would be to have multiple submit buttons inside the same form:

@using (Html.BeginForm("actionName", "controllerName"))
{
    <button type="submit" name="a" value="action1">action 1</button>
    <button type="submit" name="a" value="action2">action 2</button>
}

and then in your controller action you could use the a parameter used as name of the button to know which button was used to submit the form:

public ActionResult ActionName(string a)
{
    if (a == "action1") 
    {
        // The first button was used to submit the form
    }
    else if (a == "action2") 
    {
        // The second button was used to submit the form
    }
    else
    {
        // No button was used to submit the form => the user simply clicked
        // on the Enter key while inside some of the input fields
    }

    ...
}

And if you wanted to dispatch to different controller actions depending on which button was clicked you could write a custom ActionNameSelectorAttribute as shown in this answer.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

in my one app i did this :

    <nav >
     <ul>
         <li>
             <a href="#" id="POSbtn">POS</a>
         </li>
         <li>
             <a href="#" id="btnQuery">Query User</a>
         </li>           
     </ul>
 </nav>

and then some javascript/Ajax:

    $("#POSbtn").click(function () {
    //alert("no");
    $.ajax({
        url: '/MainScreen/POS/',
        contentType: 'application/html; charset=utf-8',
        type: 'GET',
        dataType: 'html',
        data: "html",
        success: function (result) {
            $("#divPOS").html(result);
        }
    })
});

Contorl looks like this :

       public ActionResult POS()
    {
        if (Request.IsAjaxRequest())
        {
            mymodel.Veh_Name = "Vehicle Groups ...";       
            return PartialView("POS",mymodel);
        }
        return View("POS");
    }

hope this helps you a bit, there is also this way i did on one other application.

HTML :

     @using (Html.BeginForm("Index", "MainScreen", FormMethod.Post))
    {
         <div class="button">
                <input type="submit" value="Log in" />
         </div><!-- button -->
    }

and obviously the above answer from Darin shows the other ways , using actionlink and all those controls

Arrie
  • 1,327
  • 5
  • 18
  • 39