Dhwani
Dhwani

Reputation: 7626

How to bind data from model to ASP Controller?

I am using asp.net mvc3(C#) for my project. I am new in this. I have a table which has data like name, age, state_id. Now I want that when I take asp control GRIDVIEW, It should bind my table's data to the gridview and also 2 column as edit and view. How can I do through MVC? I am totally lost in this.

Again I have another two table

1)Country_Master has column country_id,country_name

2)City_Master has column state_id,state_name.

I want that when I select country in dropdown list, its corresponding state list should be display in another dropdownlist.

Upvotes: 1

Views: 1105

Answers (2)

Iswanto San
Iswanto San

Reputation: 18569

For the gridview, you can use MvcContrib Grid or JQuery Grid for MVC

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039060

when I take asp control GRIDVIEW, It should bind my table's data to the gridview and also 2 column as edit and view. How can I do through MVC?

I think that you misunderstood some fundamental concepts in ASP.NET MVC. There are no longer any server side controls such as GridView. In ASP.NET MVC there's no longer ViewState nor PostBack models which were used in classic WebForms. For this reason none of the server side controls you might have used in WebForms work in ASP.NET MVC. It is a completely different approach to web development.

In ASP.NET MVC you could start by defining a Model that will hold your data:

public class PersonViewModel
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Country { get; set; }
}

then a Controller that will talk to your DAL and populate the model:

public class PersonController: Controller
{
    public ActionResult Index()
    {
        IEnumerable<PersonViewModel> model = ... talk to your DAL and populate the view model
        return View(model);
    }
}

and finally you have a corresponding view where you could display the data of this model:

@model IEnumerable<PersonViewModel>
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Country</th>
        </tr>
    </thead>
    <tfoot>
    @foreach (var person in Model)
    {
        <tr>
            <td>@person.Name</td>
            <td>@person.Age</td>
            <td>@person.Country</td>
        </tr>
    }
    </tfoot>
</table>

In ASP.NET MVC views you could also use some of the built-in helpers. For example there's a WebGrid helper allowing you to simplify the tabular output:

@model IEnumerable<PersonViewModel>
@{
    var grid = new WebGrid();
}

@grid.GetHtml(
    grid.Columns(
        grid.Column("Name"),
        grid.Column("Age"),
        grid.Column("Country")
    )
)

I would recommend you going through the getting started tutorials about ASP.NET MVC to better familiarize yourself with the basic concepts.

Upvotes: 1

Related Questions