Posts

Showing posts with the label ASP.net MVC

Creating page for MVC in C#.net .

For this initially we need to have a model. Let the model be Cust for Customer public class Cust() { [DisplyName= "CustmerId"] public int Cust_Id{get ; set;} [DisplayName= "Cusotmer Name"] public string Cust_Name {get; set;} [DisplayName = "Cusomter Nick Name"] public string Cust_Nickname{get; set;} } Now we need to create a controller . Let the controller name Be Customer Initially we need to add the customer to the database To create any customer using this application we need to create two action methods, First will generate the html for the user to view the page to be created. Another, this methods will take the arguments from the UI(creation page) [HttpGet] public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(Cust NewCustomer) { //var date= DateTime.Today; // this may be anything you nee to pass on to next method // Do some thing on the code   return View("Cust"); //return...

Create a DataTable from a Class using reflection .

There are many solution to this problem, For this we use a technical process called Reflection. Reflection: To dynamically create an instance  of a type, bind the type to an existing  object ad invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them. 1. Use AsEnumerable()  method to support LINQ: private List<T> ConvertToList<T>(DataTable dt)     {         var columnNames = dt.Columns.Cast<DataColumn>()             .Select(c => c.ColumnName)             .ToList();         var properties = typeof(T).GetProperties();         return dt.AsEnumerable().Select(row =>             {                 var objT = Activator.CreateInstance<T>();         ...

How to pass model in a ajax call to a method ??

Form MVC 3 or MVC 2 the objs passed in the form of json are automatically converted model format if the model of that type of object exists. Example Let say we have a model 'Customer' Public class Customer { public int CustomerID {get; set;} public string CustomerName {get; set;} public string CustomerAddress {get; set;} } Here we have a model 'Customer' with 3 attributes ID, Name, Address. Now we need to have a Get method and a post method public ActionResult Index() { return View(""); } [HttpPost] public ActionResult Index( Customer modelCustomer) { // Do your stuff here return View ("") } Here i have two different Function in the above code one is the httpGet method this is used to call the view where as the other is used to call the post method form the view may be using Submit or using ajax calls. here goes the code for ajax call @model iEnumerable @using (Html.BeginForm("Index", "Home" , FormMethod.Post , ...

Passing multiple models in single view .

There are two different ways of doing this . * Passing parent view that contains both the models which we want to pass in the view . * passing each model in a ViewBag we have two different models public class Customer { public int CustomerID {get; set;} public string CustomerName {get; set;} } public class Product { public int ProductId {get; set ;} public string ProductName{get; set ;} } 1. By passing a parent view Here we want to pass both the models in a single view , by default the razor engine only accepts single model per view. so we need to create a parent view that hold both the models. public class ParentView { public Customer customer{get; set;} public Product product{get ; set;} } then we can call the model in the view by using the following code in view page : @model iEmumerable <models.ParentView> ForEach(var v in ParentView) { < option > @v.customer.CustomerID < / option > < option > @v.customer.CustomerName <...

A Briefing to MVC

Image
MVC  Its is a framework for better web application development . Before this used the Web Forms for the purpose of web application creation. Web application creates has a long runny heavy process in which the controller is connected and maintained by server. The details of every small control is connected and shared with server at every point. Thus MVC was introduced to get the a light weight web application. its uses the HTML controls to make the page . In web forms the controls are converted to the Html tags at run time but in MVC the the Html is created at the design time thus making it faster than any other web forms based application .  MVC has some positive points over the Web Forms   It provides better structure to web applications As controller created in MVC are class of  .Net thus its is easy to create unit cases and the automatic manual test cases. it can provide a better layored architecture to the user to work parallel on same application...

How to post data in MVC using form collection for Beginners.

Here we can simply put the data into the form tag and give each component an name .We are creating a login form using the MVC Architecture. <form method="post" action="Name_of_the_function> <input type="text" name=username id=username> <input type="password" name =pass id=pass> <input id=submit type="submit"> on controller [httppost] public ActionResult Name_of_the_function(formcollection form) { string str=form["username"]; string str1= form["pass"]; }