Using DataSet As Data Model in asp.net MVC 4 Proje

2019-09-17 12:23发布

问题:

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:

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 -