Tuesday, 22 February 2011

Asp.net mvc having multiselect with the listbox

Populate the Listbox with MultiSelect.
Controller:
  public ActionResult Create()
  { 
     var Degree=DegreeRepository.GetAllDegree();      
     ViewData["Degree"] = new MultiSelectList(Degree, "DegreeId", "DegreeName");
     return View(); 
  }

Here I want to mention if you want the single selection for the Listbox you can use SelectList as I mentioned here

View
<%: Html.ListBoxFor(model => model.DegreeId, ViewData["Degree"] as MultiSelectList) %>  

Population and then selection of item in the dropdownList. 

Controller:

  public ActionResult Edit(int id)
  {       
     var Users=UserRepository.GetUser(id);    
     var Degree=DegreeRepository.GetAllDegree();   
     ViewData["Degree"] = new SectList(Degree,"DegreeId","DegreeName",Users.DegreeId);
     return View();
  }


View:

<%: Html.DropDownListFor(model => model.DegreeId, ViewData["Degree"]) %>

Asp.net mvc Dropdownlist population and selection

Populate the dropdownList.

Controller

     public ActionResult Create()
     {       
        var Degree=DegreeRepository.GetAllDegree();
        ViewData["Degree"] = new SelectList(Degree, "DegreeId", "DegreeName");
        return View();
     }

View

<%: Html.DropDownListFor(model => model.DegreeId, ViewData["Degree"]%>

Population and then selection of item in the dropdownList.

Controller

   public ActionResult Edit(int id)
   {       
     var User=UserRepository.GetUser(id);  // Get Single User
     var Degree=DegreeRepository.GetAllDegree();   // Get All Degrees
     ViewData["Degree"] = new selectList(Degree,"DegreeId",DegreeName",User.DegreeId);  
     return View();
   }

View
<%: Html.DropDownListFor(model => model.DegreeId, ViewData["Degree"]  %>

Friday, 11 February 2011

Asp.net mvc c# changing the dateformat

There are times when you need to change the datetime format of the default date generated by System.DateTime.Now to some other.

Here how you can do it
string datetFormat = "dd-MMMM-yyyy hh:mm";
string date = Convert.ToDateTime(datetime).ToString(datetFormat);
It will generate 11-February-2011 06:13

Wednesday, 9 February 2011

How to use Asp.net mvc JavaScriptResult practically

I am usually very excited about knowing something that can make my life easy and JavaScriptResult is one of that thing. It is used to execute JavaScript code immediately on the client sent from the server. Here is the small code snippet for its implementation.

View: 
<%: Ajax.ActionLink("display", "Display", new AjaxOptions()) %>
<%: Html.TextBox("name","mazhar") %>
<%: Ajax.ActionLink("checks", "Checks", new AjaxOptions()) %> 

Controller:
  public ActionResult Display()
  {        
    var  script = "$('#message').append('Display');";
    return JavaScript(script);
  }

  public ActionResult Checks()
  {        
    string script = "var textboxvalue=$('#name').val();";
    script += "$('#message').append(textboxvalue);";
    return JavaScript(script);
  } 

Output:

1) Clicking on the first link will display Display in the message div.
2) Clicking on the second link will display mazhar in the message div.

It's usage in the light of Asp.net Mvc:

Asp.net mvc is the design pattern which is all about separation of concern . It has the model, view and controller. Each doing it part and not intermingling with each other directly. My Advice would be to not use it but this thing is present if it become necessary.