2011-07-06 25 views
5

using ifadesinin, jilet görünümlerinde genel yöntemlerle çalışmasını sağlamanın bir yolu var mı? Mesela ben bir HTML etiketi olarak jilet yorumladığını <Controller> beri, webforms Jilet görünümünde genel yöntemlerle kullanma

<% using(Html.BeginForm<Controller>(c => c.Method())) 
    { %> 
     Some code 
<% } %> 

bu

@using(Html.BeginForm<Controller>(c => c.Method())) 
{ 
    Some code 
} 

gibi ustura dönüştürülür pasajı ama bu işe yaramazsa istiyorum. Parantez eklemek de işe yaramaz, çünkü o zaman jilet, BeginForm'u başlatan ve sonlandıran küme parantezlerini içermez. Aşağıda denediğim farklı yaklaşımlar var ve daha fazla düşünemiyorum.

@using(Html.BeginForm<Controller>(c => c.Method())) // Interpreted as c# to '<Controller>' 
{             // interpreted as HTML 
    Some code          // interpreted as HTML 
}             // interpreted as HTML 

@(using(Html.BeginForm<Controller>(c => c.Method()))) // Interpreted as c# 
{              // interpreted as HTML 
    Some code           // interpreted as HTML 
}              // interpreted as HTML 

@{using(Html.BeginForm<Controller>(c => c.Method())) // Interpreted as c# 
    {            // interpreted as c# 
     Some code         // interpreted as c# 
    }            // interpreted as c# 
}             // interpreted as c# 

@(using(Html.BeginForm<Controller>(c => c.Method()))) // Interpreted as c# 
@{             // interpreted as c# 
     Some code          // interpreted as c# 
}              // interpreted as c#  

Aynone bunu nasıl yapacağını biliyor mu?

Güncelleştirme: Yukarıdaki üçüncü yolun böyle yapılacağı anlaşılıyor. Razor, şu şekilde çalışır:

@{using(Html.BeginForm<Controller>(c => c.Method())) // Interpreted as c# 
    {            // interpreted as c# 
     <p>           // interpreted as HTML 
     Some text         // interpreted as HTML 
     @Code          // interpreted as c# 
     </p>           // interpreted as HTML 
    }            // interpreted as c# 
}             // interpreted as c# 

İşleri yapmanın en açık yolu değil, ama işe yarıyor.

Güncelleme 2: beklendiği gibi şimdi opsiyon 1. Yukarıdaki çalıştığı için Jilet muhtemelen bir noktada güncellendi.

@using(Html.BeginForm<Controller>(c => c.Method())) 
{ 
    Some code 
} 
+0

Sonunda gayet iyi çalışıyor gibi görünüyor. Ama hiçbir "BeginForm " bulunamadı (test etmek için hızlı bir yardımcı yöntem yaptım), bu yüzden harici bir kütüphane mi kullanıyorsunuz? Razor sözdizimindeki hata nedir? – Buildstarted

+0

Güçlü yazılmış html yardımcılarını içeren Microsoft.Web.Mvc.dll dosyasından MvcContrib uzantıları kullanıyoruz. Hata @ (Html.BeginForm (...)) iyi çalışıyor ama @using (Html.BeginForm (...)) değil ve bu şekilde biçimlendirmek için bir yol anlayamıyorum iş. –

cevap

7

Ne Pars bütün ifadeyi sarın eğer:

@(using(Html.BeginForm<Controller>(c => c.Method()))) 

Veya bir kod bloğu kullanılır?

HTH.

+0

Bunu denedim ve bu iyi değil. Yukarıdaki farklı denemelere örnekler eklendi. –

+0

MVC 4.5'te benim için çalışıyor. –

İlgili konular