SORU
14 Ocak 2009, ÇARŞAMBA


Nasıl başa çoklu ASP.NET MVC Çerçevesinde düğmeleri razı mısınız?

Biraz kolay bir şekilde işlemek için birden fazla aynı formdan gönder düğmeleri var mı? Örnek:

<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" value="Send" />
<input type="submit" value="Cancel" />
<% Html.EndForm(); %>

ASP.NET Çerçeve Beta bunu yapmak için nasıl bir fikriniz var mı? İçin Google'da tarattım örnekleri tüm bunları tek düğmeli.

CEVAP
14 Ocak 2009, ÇARŞAMBA


Düğmeler bir isim göndermek ve kumanda yöntemi: sunulan değer ver inceleyin

<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" name="submitButton" value="Send" />
<input type="submit" name="submitButton" value="Cancel" />
<% Html.EndForm(); %>

yazabilmek için

public class MyController : Controller {
    public ActionResult MyAction(string submitButton) {
        switch(submitButton) {
            case "Send":
                // delegate sending to another controller action
                return(Send());
            case "Cancel":
                // call another action to perform the cancellation
                return(Cancel());
            default:
                // If they've submitted the form without a submitButton, 
                // just return the view again.
                return(View());
        }
    }

    private ActionResult Cancel() {
        // process the cancellation request here.
        return(View("Cancelled"));
    }

    private ActionResult Send() {
        // perform the actual send operation here.
        return(View("SendConfirmed"));
    }

}

DÜZENLEME:

Bu yaklaşım, yerelleştirilmiş siteleri ile çalışmak için genişletir, mesajları başka bir yere (örneğin, kesinlikle yazılan kaynak bir sınıf için kaynak dosyası derleme) izole

Sonra kodu değiştirme gibi çalışır:

<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" name="submitButton" value="<%= Html.Encode(Resources.Messages.Send)%>" />
<input type="submit" name="submitButton" value="<%=Html.Encode(Resources.Messages.Cancel)%>" />
<% Html.EndForm(); %>

ve denetleyicisi bu gibi görünmelidir:

// Note that the localized resources aren't constants, so 
// we can't use a switch statement.

if (submitButton == Resources.Messages.Send) { 
    // delegate sending to another controller action
    return(Send());

} else if (submitButton == Resources.Messages.Cancel) {
     // call another action to perform the cancellation
     return(Cancel());
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • ASUS

    ASUS

    22 EKİM 2005
  • booba1234

    booba1234

    22 Temmuz 2006
  • TopDJMag TV

    TopDJMag TV

    29 Temmuz 2010