Friday, 24 August 2012

Using ViewBag and for sql server insertion Method:

In mvc we ve three objects are available as properties of both view and controller these purpose for transfering data from controller to view or in between two views...  here viewbag usefull for while in controllers we did to catch that data in a viewbag then i'm showing data in another view while returing that index... jst see follow...


         Contact obj = new Contact();
             
                obj.name = Request.Form["myname"];
                obj.email = Request.Form["Email"];
                obj.telephone = Request.Form["Telephone"];
                obj.createcontact(obj);
                Contact conReturn = new Contact();
                conReturn = obj.createcontact(obj);
                ViewBag.contact = obj;
               return View("thanq");

 In the above i done inserted my fields data into DB in that  Createcontact() method  as well then i returned the object to here  and catching here in "Viewbag.Contact" then i'm returing to the view called "thanq", in "thanq" page the viewbag usage is as follows 

        @ViewBag.contact.name<br/>
        @ViewBag.contact.email<br/>
        @ViewBag.contact.telephone<br/>

Here it'll shows the out put that the viewbag object that which it contains the data about name,email,telephone as well which the class model objects created in model those were be accessible here.. in my model i created my model called contact as like 

 public class Contact
    {
        public string name { get; set; }
        public string email { get; set; }
        public string telephone { get; set; }

    }


No comments:

Post a Comment