Sunday, February 27, 2011

Cascading dropdown in asp.net mvc

     First in the view page “Index.aspx”  need  to write the JQUERY  codes to generate cascade dropdown list as follows:
<script type="text/javascript" >
   $(document).ready(function()              //This will load fuction in page load itself
   {
      $("#Category").change(function()      //First dropdown function on change
         {                
            LoadSecond($("#Category").val());    //Function to load second dropdownlist
                 }).change();
             });
             function LoadSecond(name)                      
                        {
                            var url = "/DDL/DDL";                 //Controller/action name
                            $.getJSON(url, { Division1: name }, function(data) {
                                $("#Division").empty();
                                $("#Division").append("<option value" + "%" + ">" + "Select" + "</option>");
                                $.each(data, function(index, DDL) {                                   
                                    $("#Division").append("<option value='" + DDL.value + "'>" + DDL.Text + "</option>");
                                });
                            });           
                       }          
   </script>

// Division-name of second dropdown
// data-Parameter name given in the function
// Division1-Parameter name given in the controller

The second dropdown need to be in "select" tag not html dropdown
<div>
   <%=Html.DropDownList("Category")%>         
 
   <select id="Division" name="ddlCamDivision" style="width:100px">

</select>
 </div>
The controller function code will be of the following:
  public ActionResult DDL(string Division1)
        {
            try
            {
                 var w = (from m in CE.seconddd
                        where m.categoryname ==Division1
                        select m.items);

                SelectList Division = new SelectList(w);            
                return this.Json(Division, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return View();
            }
        }

Tuesday, February 22, 2011

MVC dropdown databinding

In controller page we need to use this code, here categoryid is VALUEFIELD and category1 is TEXTFIELD then the last parameter is default value to be displayed:

SelectList Category = new SelectList(CE.category,"categoryid","category1", "Select the category");

 
ViewData["Category"] = Category;

Tuesday, February 1, 2011

Writting name to the word document

using Microsoft.Office.Core;
using Microsoft.Office;
using Microsoft.Office.Interop.Word;



void InsertInWordDoc(string name)
        {
            try
            {
                string filename = (fpUploadresume.FileName.Substring(fpUploadresume.FileName.LastIndexOf(".") + 1));
                if ((filename == "doc") || (filename == "docx"))
                {
                    if(txtReferredby.Text!="")
                    {
                        object Missing = System.Reflection.Missing.Value;
                        object fileToOpen = (object)PathToSave;
                        Application app = new ApplicationClass();
                        Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
                        doc = app.Documents.Open(ref fileToOpen, ref Missing, ref Missing, ref Missing, ref Missing,
                        ref Missing, ref Missing, ref Missing, ref Missing,
                        ref Missing, ref Missing, ref Missing, ref Missing,
                        ref Missing, ref Missing, ref Missing);
                        Range rg = doc.Range(ref Missing, ref Missing);
                        rg.InsertBefore("Referred by: " + name);
                        rg.Font.Name = "times new roman";
                        rg.Font.Size = 14;
                        rg.InsertParagraphAfter();
                        rg.InsertParagraphBefore();
                        app.Documents.Save(ref Missing, ref Missing);
                        doc.Close(ref Missing, ref Missing, ref Missing);

                    }

                }
            }
            catch (Exception ex)
            {
            }
        }