3

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

0

1 Answer 1

4

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 -

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

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
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.