Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing the return value of a method to an Action Result which is to be displayed in a label on the view using mvc 3 c#
    text
    copied!<p>I will like to pass the variable "totalCount" to my view and let this display in a label or text area when my view loads. Here is my code below. How can I achieve this in Mvc 3?</p> <p>Here is my class </p> <pre><code>public class person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public person(int myId, string myFirstname, string myLastname, string myAddress) { Id = myId; FirstName = myFirstname; LastName = myLastname; Address = myAddress; } } </code></pre> <p>Here is my controller .... </p> <pre><code>namespace WriteObjectToDb.Controllers { public class HomeController : Controller { public ActionResult Index() { int result; List&lt;person&gt; mn = new List&lt;person&gt;(); mn.Add(new person(1,"john", "adebayo", "shobanjo close jericho")); mn.Add(new person(2, "david", "johnson", "ibada close")); mn.Add(new person(3, "bayo", "olomale", "oluyole estate ringroad")); result = WriteToDb(mn); return View(mn); } public ActionResult About() { return View(); } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="mn"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; private int WriteToDb(List&lt;person&gt; mn) { int totalCount; string connect = string.Empty; using (TransactionScope scope = new TransactionScope()) { string sqlIns = "insert into table(firstname,lastname,address) values(@firstname,@lastname,@address)"; SqlConnection cn = new SqlConnection(connect); SqlCommand cmdIns = new SqlCommand(sqlIns,cn); for(int i=0;i&lt;mn.Count;i++) { cmdIns.Parameters.AddWithValue("@firstname",mn[i].FirstName); cmdIns.Parameters.AddWithValue("@lastname",mn[i].LastName); cmdIns.Parameters.AddWithValue("@address",mn[i].Address); cmdIns.ExecuteNonQuery(); cmdIns.Parameters.Clear(); } scope.Complete(); totalCount = mn.Count(); return totalCount; } } } } </code></pre> <p>Here is my view.....</p> <pre><code>@model IEnumerable&lt;WriteObjectToDb.Models.person&gt; @{ ViewBag.Title = "Index"; } &lt;h2&gt;Index&lt;/h2&gt; &lt;p&gt; @Html.ActionLink("Create New", "Create") &lt;/p&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt; FirstName &lt;/th&gt; &lt;th&gt; LastName &lt;/th&gt; &lt;th&gt; Address &lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; @foreach (var item in Model) { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.FirstName) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.LastName) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Address) &lt;/td&gt; &lt;td&gt; @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | @Html.ActionLink("Details", "Details", new { id=item.Id }) | @Html.ActionLink("Delete", "Delete", new { id=item.Id }) &lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload