What is the best way to use multiple buttons in an ASP.NET MVC form?

I have the feeling that I am also “web forms” with this, but I will ask anyway. I have a form in an ASP.NET MVC project with several input fields and two buttons. One button is used to filter one of the lists. Another is used to submit the form. My view looks something like this:

<%using (Html.BeginForm())
  {%>
   <%=Html.TextBox("SearchItems") %>
   <input type="submit" name="Command" value="Find" />
   <%=Html.ListBox("SelectedItems", new MultiSelectList(model.AvailableItems,"Id", "Name", model.SelectedItems))%>
   //Some other form fields.
   <input type="submit" name="Command" value="Send" />
    <%} %>

My actions look something like this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index([Bind(Prefix = "")]SendMessageRequest model)
    {
        if (model.Command == "Find")
            return SearchItems(model);
        if (model.Command == "Send")
            return Send(model);
        throw new Exception("Invalid command.");
    }

- , . , . , . , → , . Pocket IE, , -, Pocket IE - - Command, .

Pocket IE, , JavaScript. Ajax , onClick.

, , MVC-? , - ?

+3
4

. , , :

(c = > c.Search(), FormMethod.Get);  {% > (c = > c.Send(), FormMethod.Post);  {% >

- . 2 . , . MVC , Webforms. , !

+5

, .

javascript ( jQuery) :

$("#myForm").attr("action", "mvcaction");
$("#myForm").submit();

, , , , . , .

EDIT: , Pocket PC JQuery , , , javascript.

+1

, , , .

We circumvented it using the $ .post () method from the jQuery library.

$("#update").click(function() {
                $.post("/Register", { name: $("#firstName").val(),
                    surname: $("#surname").val(),
                    username: $("#email").val(),
                    accountName: $("#accountName").val(),
                    password: $("#password").val()
                },
                  function(data) {
                      alert(data);
                  });
            });

And the cancel button sent fewer values ​​back.

$("#cancel").click(function() {
                $.post("/Cancel", { username: $("#email").val(),
                    password: $("#password").val()
                },
                  function(data) {
                      alert(data);
                  });
            }); 

Good again, relying on javascript inclusion, but if you are going to disable javascript, you better not use the internet (and my application, because it will be heavy for js).

0
source

All Articles