ASP.Net MVC Error: Only one model statement is allowed in a file

Last Reply 3 months ago By dharmendr

Posted 3 months ago

hi

I used below codes to show data in table:

 Controller:

        public ActionResult List()
        {
            DataSet ds = new DataSet();
            string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "SELECT * FROM Resellers";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        sda.Fill(ds);
                    }
                }
            }

            return View(ds);      
        }

 

View:

@using System.Data
@model DataSet

<div id="divSearch">
    <table class="table-full table-total">
        @foreach (DataRow row in Model.Tables[0].Rows)
        {
            <tr>
                <td>
                    @row["Name"]
                </td>
                <td>
                    @row["Address"]
                </td>
            </tr>
        }
    </table>
</div>

it will shows data from database in Table

now I want bind 2 dropdownlist in this view so I worte below code:

        private static List<SelectListItem> PopulateOstan()
        {
            SqlConnection constr = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Constring"].ConnectionString);
            List<SelectListItem> items = new List<SelectListItem>();
            using (SqlCommand cmd = new SqlCommand("SelectOstan", constr))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Connection = constr;
                constr.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        items.Add(new SelectListItem
                        {
                            Text = sdr["Name"].ToString(),
                            Value = sdr["id"].ToString()                          
                        });
                    }

                    constr.Close();
                }
                return items;
            }
        }

        public ActionResult List()
        {
            var model = new SearchViewModel();
            model.Ostans = PopulateOstan();
            model.Citeies = PopulateCiteies1();
            return View(model);       
         }

SearchViewModel

public class SearchViewModel
{
    public IEnumerable<Reseller> Reseller1 { get; set; }
    public DataTable Reseller { get; set; }
    public List<SelectListItem> Ostans { get; set; }
    public List<SelectListItem> Citeies { get; set; }
}

now I want use both Dateset and viewmodel in one view so I should wrote below codes:

@model MvcInternetShop.ViewModels.Test.SearchViewModel
@using System.Data
@model DataSet

but it makes below error

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.  Parser Error Message: Only one 'model' statement is allowed in a file. Source Error: 

Line 3:  @model MvcInternetShop.ViewModels.Test.SearchViewModel
Line 4:  @using System.Data
Line 5:  @model DataSet
Line 6:  
Line 7:  @{

 

You are viewing reply posted by: dharmendr 3 months ago.
Posted 3 months ago

Hi nedash,

Refer below code.

Model

public class SearchViewModel
{
    public IEnumerable<Reseller> Reseller1 { get; set; }
    public DataTable Reseller { get; set; }
    public List<SelectListItem> Ostans { get; set; }
    public List<SelectListItem> Citeies { get; set; }
}

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        var model = new SearchViewModel();
        model.Ostans = PopulateOstan();
        model.Citeies = PopulateCiteies1();
        model.Reseller = PopulateReseller();
        return View(model);
    }

    private static List<SelectListItem> PopulateOstan()
    {
        return new List<SelectListItem>();
    }

    private static List<SelectListItem> PopulateCiteies1()
    {
        return new List<SelectListItem>();
    }
    private static DataTable PopulateReseller()
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("Name"), new DataColumn("Address") });
        dt.Rows.Add("John Hammond", "United States");
        dt.Rows.Add("Mudassar Khan", "India");
        dt.Rows.Add("Suzanne Mathews", "France");
        dt.Rows.Add("Robert Schidner", "Russia");

        return dt;
    }
}

View

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<_DataSet_ViewModel_MVC.Models.SearchViewModel>" %>

<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Index</title>
</head>
<body>
    <div id="divSearch">
        <table class="table-full table-total">
            <%  foreach (DataRow row in Model.Reseller.Rows)
                {%>
            <tr>
                <td><%=row["Name"] %></td>
                <td><%=row["Address"] %></td>
            </tr>
            <% }%>
        </table>
    </div>
</body>
</html>

Screenshot