Home » Asp.netRSS

asp.net mvc multi button on same page problem

On my employee edit view page i have 3 buttons Save,Cancel,Delete.

Save - saving view page values. calling controller action.

Cancel - Go back to previous view page. (List of employee page. where this page view comes)

Delete - Delete current page employee and go back to List of employee page. where this page view comes.


Save button click by default working for me calling controller.

but when i click cancel and delete button same controller gets call

How to handle this situation on page view.


 

4 Answers Found

 

Answer 1

Hi,

In all cases, you could call the same controller  but return different ActionResult values  depending on the clicked button. May I ask what is the problem  for that? It sounds like you have different controllers for different actions - which I believe is not the best approach since all the buttons  are related to the management of "Employee" entities. Sorry if I misunderstood your question.

BR.

 

Answer 2

on my employe detail  page i have 3 buttons

Save             Cancel               Delete


Save button  save current employee  property from page.

Cancel- back  to employee list

Delete - delete  current employee and go back to employee list.


Please let me know how can i hande this... as by calling  different ActionResult.


 

Answer 3

same controller  and different action  result is ok...

following way implement this

in controller

public ActionResult Index2()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            return View();
        }
        public ActionResult Index3()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            return View();
        }

public ActionResult Index2()

        {

            return View("Index");

        }

        public ActionResult Index3()

        {

            return View("Index");

        }


in View 

<% using (Html.BeginForm("Index2", "Home"))
       {%>
    <p>
        <input type="submit" value="Save2" />
    </p>
    <% } %>
    <% using (Html.BeginForm("Index3", "Home"))
       {%>
    <p>
        <input type="submit" value="Save3" />
    </p>
    <% } %>

But my buttons  on page  is not coming properly in one row....

 

Answer 4

Here is how I did it.

<% using (Html.BeginForm())
               { %>


<input type="submit" class="savebutton" style="display: block; " value="" />

<a href='<%: Url.Action("Calendar")%>' style="text-decoration: none; color: #000;" >
                    <%= Html.Image("buttons/btn_cancel.png")%>
</a>

<a href='<%: Url.Action("DeleteReservation", "App", new { id = Model.Reservation.Id })%>' style="text-decoration: none; color: #000;" >
     <%= Html.Image("buttons/btn_delete.png")%>
</a>

<% } %>


1) THE SAVE BUTTON POSTS THE FORM

2) THE CANCEL BUTTON DIRECTS TO ANOTHER CONTROLLER WITHOUT SAVING ANY DATA IE. CANCEL

3) THE DELETE BUTTON LINKS TO A NEW CONTROLLER AND SETS THE ID OF WHAT EVER YOU ARE DELETING.  GENERALLY TO DELETE YOU DON'T NEED TO SEND THE WHOLE FORM YOU JUST NEED THE OBJECT ID YOU ARE DELETING.  WITH THAT DATA YOU CAN DELETE ANY THING LINKED TO THAT OBJECT ID.


NOTE: <%= Html.Image("buttons/btn_delete.png")%> This basically just <img src="/Public/images/buttons/btn_delete.png" />

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter