Friday, October 26, 2012

MVC -SelectedList

Today we will see a small example of how to use SelectedList. It comes under the namespace System.Web.Mvc.

This list basically helps you to represent a list that let user to select item.

To start with we will create a Model say SelectListExampleModel.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SubmitTest.Models
{
    public class SelectListExampleModel
    {
        public SelectListExampleModel()
        {
            if (Items == null)
            {
                Items = new List<Item>();
            }
        }
        public SelectList ItemList { get; set; }
        public List<Item> Items { get; set; }
        public int ItemList1 { get; set; }
    }
    public class Item
    {
        public int ItemId { get; set; }
        public string ItemName { get; set; }
    }
}

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SubmitTest.Models;

namespace SubmitTest.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            //Lets create a list of items here
            

            var selectedList = new SelectListExampleModel();
            Item item = new Item();
            item.ItemId = 1;
            item.ItemName = "Bread";
            selectedList.Items.Add(item);
            Item item1 = new Item();
            item1.ItemId = 2;
            item1.ItemName = "Butter";
            selectedList.Items.Add(item1);
            selectedList.ItemList = new SelectList(selectedList.Items, "ItemId", "ItemName");

            return View(selectedList);
        }
        [HttpPost]
        public ActionResult Index(SelectListExampleModel model)
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

          

           var selected= model.ItemList1.ToString();


           return View(model);
        }

        public ActionResult About()
        {
            return View();
        }
    }
}


Lets have a view Named Index.cshtml

@model SubmitTest.Models.SelectListExampleModel

@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
 @using (Html.BeginForm())
 {
     @Html.DropDownList("ItemList1", Model.ItemList, "- Please Select -")
     <input type="submit" value="Get Selected Value" />
     
 }
  

Happy Coding :)

Friday, October 19, 2012

IE Issue -HTML 5 Button with Jquery submit() event


If you are using the HTML 5 button tag and inside it you are calling submit() that is of Jquery, In IE browser you will notice that the event gets trigger 2 times.This seems to be issue with  browser.It works well on Firefox,chrome.

Basically do not use the following syntax inside the form tag  if you are  supporting the IE else you will have to do some trick to avoid first or second call.

@using (Html.BeginForm()){
<button onclick="submit()">"Create"</button>
}

Happy Coding :)

Thursday, October 18, 2012

Difference Between Firebug and Fiddler

In this Article we will some differences between Firebug and Fiddler

Fiddler and Firebug are two different tools that we use to solve two different problems.


Firebug 
Firebug is primarily used to Inspect / Tweak the DOM, script debugging, and basic network information. It primarily deals with things inside the browser.
Some things you would use Firebug for is tweaking CSS styles, trying to figure out where some style is coming from, etc. It can also be used for breakpoints in JavaScript, and basic request / response information such as content, headers, timing, etc.
Fiddler
Fiddler goes much further underneath of it. Fiddler is useful for inspecting network / protocol level information that the browser is doing, as well has a lot of analysis helpers.We typically use this when We are trying to diagnose compression issues, see network traffic, very fine grained details for timing, or making your own requests to the server, which we do when we are tinkering with SOAP / Web Services.