Archive for the ‘JQuery’ Category
Asp.Net MVC Veri Aktarım Elemanı
Merhaba, MVC ile site geliştiriyorsanız benim karşılaştığım durum ile sizde karşılaşabilirsiniz. Burada olay şöyle gelişiyor:
1. Kişi yeni veri giriş ekranında bir veri giriyor.
2. sistem verinin doğruluğundan emin olduktan sonra veriyi veri tabanına kaydediyor.
3. sonrada işlem listeleme sayfasına devrediliyor.
bu işleme kod olarak baktığımızda :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Ornek : Controller { public ActionResult Index() { var veriler = VeriKaynagi.VerileriAl(); return View(veriler); } public ActionResult Yeni() { return View(new Veri()); } [HttpPost] public ActionResult Ekle(Veri veri) { if (!ModelState.IsValid) { return View("Yeni", veri); } // veriyi veri tabanına kaydeden kodlar return RedirectToAction("Index"); } } |
Şimdi yapmamız gereken veri doğru olarak kaydedildiğine dair bir bilgiyi sayfaya göndermek istiyorum. Ama bunuda yapıyı bozmadan yapmam lazım. O zaman ilk aklıma ViewBag kullanmak geldi ve kodu şöyle düzelttim :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Ornek : Controller { public ActionResult Index() { var veriler = VeriKaynagi.VerileriAl(); return View(veriler); } public ActionResult Yeni() { return View(new Veri()); } [HttpPost] public ActionResult Ekle(Veri veri) { if (!ModelState.IsValid) { return View("Yeni", veri); } // veriyi veri tabanına kaydeden kodlar ViewBag.EkMesaj = "Veriler Başarıyla Kaydedilmiştir."; return RedirectToAction("Index"); } } |
Çok zekice gelmişti bana ama maalesef kod çalışmadı. Daha doğrusu çalıştı ama ViewBag.EkMesaj her durumda boş geldi. Böyle olunca da Böyle Gitmez Söz Google’da dedim ve bir arama yaptım. Bu tür konularda çoğunlukla karşıma çıkan Stack Overflow‘da aradığım cevabı buldum.
Burada ViewBag’in doğru nokta olmadığı ve bunun yerine TempData elemanının kullanılması gerektiği yazıyordu. Böylece özellikle Redirect işlemlerinde ilk sayfa ikinci sayfaya bir data aktarabiliyordu. İlgili link bu.
O zaman çözüm şöyle olacak:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Ornek : Controller { public ActionResult Index() { var veriler = VeriKaynagi.VerileriAl(); return View(veriler); } public ActionResult Yeni() { return View(new Veri()); } [[HttpPost]] public ActionResult Ekle(Veri veri) { if (!ModelState.IsValid) { return View("Yeni", veri); } // veriyi veri tabanına kaydeden kodlar TempData.Add("EkMesaj", "Veriler Başarıyla Kaydedilmiştir."); return RedirectToAction("Index"); } } |
İyide ön tarafı nasıl yazarız derseniz… ben biraz atraksiyonlu yazdım. Adım adım bakalım. Önce mesajın gözükeceği yeri ayarlamamız lazım. genel geçer bir durum olsun diye ben bu elemanı master layout içine aldım. <body> tagından önceye şöyle bir kod ekledim:
böylece mesaj geldiğinde böyle gözükecek. Bu elemanın görüntü özellikleri için css’e aşağıdakini ekledim:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #dEkMesaj { display: none; position: fixed; top: 0; left: 50%; margin: 0 0 0 -100px; width: 200px; background: yellow; color: black; border: 1px solid darkorange; border-top: 0; text-align: center; } |
iyi gidiyoruz ama daha bitmedi. Birde olayın atraksiyon tarafını halleden sevgili javascript kodumuz var. Doğal olarak jquery kullanıyoruz:
1 2 3 4 5 6 7 8 9 | $(function () { if ($('#dEkMesaj').length > 0) { $('#dEkMesaj') .css({"opacity": 0, "display":"block"}) // görünmez elemanı önce görünür ama görünmez yapıyoruz :D .animate({ opacity: 1 }, 500) // sonrada yarım saniyede görünür hale geliyor. .delay(3000) // 3 saniye görüntüde kalıyor .animate({ opacity: 0 }, 1000, function () { $('#dEkMesaj').hide(); }); // sonrada görüntüden 1 saniyede çıkıyor. } }); |
bu mudur? bence budur.
Kolay gele…
Uploadify ile Firefox upload problemi
Uploadify dosya yüklemesi için kullandığımız en güzel araçlardan biri. Ancak geçen gün bu sistemle dosya yüklemesi yaparken bir problem ile karşılaştım. Yükleme işlemi başlamadan HTTP 302 hatası veriyordu. Daha doğrusu bu hata değil biliyorsunuz. Sistemde session cookie’si bulunamadığı için bizi login sayfasına yönlendiren bir HTTP geri dönüş değeri. Bu durum ile ilgili internette bir araştırma yaptım ve bunun flash plugin’lerinden kaynaklanan bir problem olduğunu öğrendim. Sorun tam olarak şu idi, flash internet explorer dışındaki plug’inlerinde browser cookie’lerini kullanmıyordu. Yani siz siteye login oldunuz, gerekli bütün işlemler ve cookie’ler browser’a yüklendi, ancak bu durumdan sonra flash ile site üzerine bir dosya göndermeye kalktığınızda o zaman flash browser’daki cookie’leri sunucuya göndermediği için flash’ın bu isteği sunucu tarafından anonim bir istek gibi algılanıp, login sayfasına yönlendirilerek sonlandırılıyor.
Tamam sorunu anladık, ama nasıl çözeceğiz. Ben uploadify kullandığım için onun için bir çözüm aradım ve şu linkteki çözümü buldum : http://stackoverflow.com/questions/1729179/uploadify-session-and-authentication-with-asp-net-mvc
Burada sevgili arkadaşımız flash’ın cookie’yi alamaması dolayısıyla cookie’de giden bilgiyi post verisi içerisine ekleyip, bu şekilde bilgiyi karşıya iletmekte. böylece cookie yok ise bu bilgi ile server flash istemcisini tanımakta. güzel bir çözüm bende bu çözümü aşağıda paylaşıyorum :
önce uploadify tanımımızda bazı bilgi eklemeleri yapmamız gerekiyor :
<script>
var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
var ASPSESSID = "<%= Session.SessionID %>";
$("#uploadifyLogo").uploadify({
...
scriptData: { ASPSESSID: ASPSESSID, AUTHID: auth }
});
Sonrada global.asax dosyasına aşağıdaki kodu eklemeliyiz :
protected void Application_BeginRequest(object sender, EventArgs e)
{
/* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */
try
{
string session_param_name = "ASPSESSID";
string session_cookie_name = "ASP.NET_SessionId";
if (HttpContext.Current.Request.Form[session_param_name] != null)
{
UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
}
else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
{
UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
}
}
catch
{
}
try
{
string auth_param_name = "AUTHID";
string auth_cookie_name = FormsAuthentication.FormsCookieName;
if (HttpContext.Current.Request.Form[auth_param_name] != null)
{
UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
}
else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
{
UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
}
}
catch
{
}
}
private void UpdateCookie(string cookie_name, string cookie_value)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
if (null == cookie)
{
cookie = new HttpCookie(cookie_name);
}
cookie.Value = cookie_value;
HttpContext.Current.Request.Cookies.Set(cookie);
}
böylece problem çözülür.
Ayrıca başka bir kaynakta MVC tarafındaki çözüm anlatılmış: http://www.uploadify.com/forums/discussion/6494/problem-with-uploadify-the-upload-does-not-start/p1
Güzel JQuery Eklentileri
Merhaba Sitelerde dolaşırken bir sitede güzel jquery eklentilerinin listelendiğini gördüm. Bunu paylaşmak istedim :
http://www.ajaxline.com/best-jquery-plugins-june-2009
işe yarıyacağını düşündüğüm bir kaç tanesi :
1. jBreadCrumb
http://plugins.jquery.com/project/jBreadCrumb –
2. jSuccest
http://www.gimiti.com/kltan/wordpress/?p=40
3. JMaps Framework
MVC Partial Rendering
MVC Partial rendering ile ilgili bulduğum birkaç güzel makalenin adresini paylaşmak istiyorum:
- Partial Rendering with ASP.NET MVC and jQuery
Yesterday, I wrote a post detailing how easy we can invoke an ASP.NET MVC controller action from JavaScript using jQuery. Lets up the ante a bit, and see if we can’t use the same approach to get some partial rendering going. To accomplish this, we’ll use a fact about the RenderViewResult class that hasn’t got a lot of attention so far – the fact that its View propetry can be set to point at a UserControl, not just a WebForm. When doing so, only the user control will be rendered – which sounds like exactly what we need for doing partial rendering.. - Partial Requests in ASP.NET MVC
In your ASP.NET MVC application, it can be tricky to combine multiple independent “widgets” on the same page. That’s because a WebForms-style hierarchy of independent controls clashes awkwardly against a purist’s one-way MVC pipeline. Widgets? I’m taking about that drill-down navigation widget you want in your sidebar, or the “most recent forum posts” widget you’d put in the page footer. Things that need to fetch their own data independently of the page that hosts them. - Ajax with the ASP.NET MVC Framework
This post presents a few basic Ajax features (similar to partial rendering and behaviors in terms of concepts) running on top of the ASP.NET MVC framework… some early ideas, experimentation and app-building results. - ASP.NET MVC: Using UserControls Usefully
This post is in response to a forum user, who is wondering how to properly use a ViewUserControl: I’m sorry if this is a really silly question, but I’m having a hard time grokking what the right usage of a ViewUserControl looks like. Does one invoke the RenderView from a controller?
eburhan.com: “jQuery ve Seçiciler”
eburhan.com adresinde JQuery ve seçiciler makalesinde JQuery ve seçiciler ile ilgili güzel bir yazı yer almakta. Bu konuya ilgili iseniz bakmanızı tavsiye ederim.
http://www.eburhan.com/jquery-ve-seciciler/
