I have a function in my controller class, it return a list of data, I want to display it in table structure in my view page. i have tried the following code but it shows some error
"Class does not contain definition of GetEnumerator"
Controller
public ActionResult data(Message msg,IEnumerable<sample> dept)
{
dbconnection db = new dbconnection();
sample s = new sample();
SqlConnection con = new SqlConnection(db.GetconString());
DataTable dt;
List<examplemvc1.Models.sample> datatable = new List<sample>();
dt = db.BuildDT("select * from MVCsample");
foreach (DataRow row in dt.Rows)
{
s.FirstName = Convert.ToString(row["First Name"]);
s.LastName = Convert.ToString(row["Last Name"]);
s.Address = Convert.ToString(row["Address"]);
s.PhoneNumber = Convert.ToString(row["PhoneNumber"]);
s.Location = Convert.ToString(row["Location"]);
datatable.Add(s);
dept = datatable;
}
ViewBag.tabledata = dept;
return View(dept) ;
}
Model
public class sample
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public string Location { get; set; }
public string tabledata { get; set; }
}
public class Message
{
public IEnumerable<sample> sampleList { get; set; }
public string MessageText { get; set; }
public string MessageFrom { get; set; }
}
View
@model examplemvc1.Models.sample
@foreach (var data in Model)
{
<table><tr><td>@data.FirstName</td></tr></table>
}
UPDATE this is how my entire view looks like
@model List<examplemvc1.Models.sample>
@{
ViewBag.Title = "Registration Form";
}
<head>
<script type="text/javascript" src="../../Scripts/jquery-1.7.1.min.js"></script>
<link href="../../Style/sample.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/samplescript.js" type="text/javascript"></script>
</head>
<h2>
Registration Form </h2>
<body>
<table>
<tr>
<th>
First Name
</th>
</tr>
@foreach (var data in Model)
{
<tr><td>@data.FirstName</td></tr>
}
</table>
@using (Html.BeginForm())
{
<table id="table1">
<tr>
<td>
@Html.Label("Enter FirstName", new { @class = "standard_label_style" })
</td>
<td>
@Html.TextBoxFor(a => a.FirstName, new { @class = "class1", title = "Enter FirstName", id = "NameBox", placeholder = "Enter name", onkeydown = "return TextField(event)" })
<span class="errorMessage"></span>
@if (!ViewData.ModelState.IsValid)
{
<span class="field-validation-error">
@ViewData.ModelState["FirstName"].Errors[0].ErrorMessage</span>
}
</td>
</tr>
<tr>
<td>
@Html.Label("Enter LastName", new { @class = "standard_label_style" })
</td>
<td>
@Html.TextBoxFor(a => a.LastName, new { @class = "class1", placeholder = "Enter name", id = "LastNameBox", title = "Enter Lastname", onkeydown = "return TextField(event); " })
<span class="errorMessage"></span>
</td>
</tr>
<tr>
<td>
@Html.Label("Enter Address", new { @class = "standard_label_style" })
</td>
<td>
@Html.TextBoxFor(a => a.Address, new { @class = "class1", id = "AddressBox", placeholder = "Enter name", title = "Enter Address" })
<span class="errorMessage"></span>
</td>
</tr>
<tr>
<td>
@Html.Label("Enter PhoneNumber", new { @class = "standard_label_style" })
</td>
<td>
@Html.TextBoxFor(a => a.PhoneNumber, new { @class = "class1", id = "PhoneBox", placeholder = "Enter name", title = "Enter Phonenumber", onkeydown = "return ValidateNumber(event);" })
<span class="errorMessage"></span>
</td>
</tr>
<tr>
<td>
@Html.Label("Enter Location", new { @class = "standard_label_style" })
</td><td>
@Html.TextBoxFor(a => a.Location, new { @class = "class1", id = "LocationBox", placeholder = "Enter name", title = "Enter Location" })
<span class="errorMessage"></span>
</td>
</tr>
</table>
<input type="button" id="btnSave" value="Register"/>
<input type="button" value="Clear"/>
<input type="button" id="Tabledata" value="Tabledata"/>
<div id="div1"></div>
@*@Html.ValidationSummary()*@
<script type="text/javascript">
function ValidateNumber(e) {
var evt = (e) ? e : window.event;
var charCode = (evt.keyCode) ? evt.keyCode : evt.which;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
};
function TextField(e) {
var evt = (e) ? e : window.event;
var charCode = (evt.keyCode) ? evt.keyCode : evt.which;
if (charCode > 31 && (charCode < 48 || charCode > 56)) {
return true;
}
else if (charCode == 8 || charCode == 9) {
return true;
}
return false
};
</script>
}
</body>
Error is shown in my view code, actually I don't know how to get that values in view page. I am newbie in mvc. Please help me to solve this. Any help will be greatly appreciated.
"Update"
I have solved My problem with the help of Stackoverflow Below is my correctcode
Controller
public ActionResult data()
{
SomeViewModel model = new SomeViewModel();
dbconnection db = new dbconnection();
SqlConnection con = new SqlConnection(db.GetconString());
DataTable dt = db.BuildDT("select * from MVCsample");
foreach (DataRow row in dt.Rows)
{
sample s = new sample();
s.FirstName = Convert.ToString(row["First Name"]);
s.LastName = Convert.ToString(row["Last Name"]);
s.Address = Convert.ToString(row["Address"]);
s.PhoneNumber = Convert.ToString(row["PhoneNumber"]);
s.Location = Convert.ToString(row["Location"]);
model.samples.Add(s);
}
return View(model);
}
Model
namespace examplemvc1.Models
{
public class sample
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public string Location { get; set; }
public string tabledata { get; set; }
}
public class Message
{
public IEnumerable<sample> sampleList { get; set; }
public string MessageText { get; set; }
public string MessageFrom { get; set; }
}
public class SomeViewModel
{
public SomeViewModel()
{
samples = new List<sample>();
sample = new sample();
}
public List<sample> samples { get; set; }
public sample sample { get; set; }
}
}
view
@model examplemvc1.Models.SomeViewModel
//................
@foreach (var data in Model.samples)
{
<tr><td>@data.FirstName</td>
<td>@data.LastName</td>
<td>@data.Address</td>
<td>@data.PhoneNumber</td>
<td>@data.Location</td></tr>
}
</table>
Just look at Stephen Muecke's Answer below he has explained the context very clearly
model List<namespace.sample>
? – Jenish Rabadiya