I want to use Data Set as my Model in my mvc 4 project. How to use the model binding with data sets. can any one give me a solution
1 Answer
You can use DataSet Like this -
Controller Action -
public ActionResult Index()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Marks");
dt.Clear();
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
DataRow dr = dt.NewRow();
dr["Name"] = "rami";
dr["Marks"] = "500";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
return View(ds);
}
Index Action -
@model System.Data.DataSet
@using System.Data
@foreach (DataRow row in Model.Tables["Marks"].Rows)
{
@(row["Name"] + " " + row["Marks"])
}
Output -

3 Comments
user3252155
thanks for the reply. but how i work with insert and updates with data sets now its give me "No parameterless constructor defined for this object." when i try to save data
ramiramilu
I dont think retrieving data is an easy job. Refer this solved thread - stackoverflow.com/questions/13375370/…. Its always better you can convert your datatable to models, and then use it in your CRUD operations.
Anthony Mason
It is true, if used as a model it needs a paramaterless constructor. So perhaps a strongly typed dataset or simply inheriting from one for your model would fix the issue.