将方法的返回值传递给Action Result,Action Result将使用mvc 3 c#在视图的标签中显示

本文关键字:Result Action 视图 显示 标签 mvc 返回值 返回 方法 值传 | 更新日期: 2023-09-27 18:19:50

我想将变量"totalCount"传递到我的视图,并让加载视图时显示在标签或文本区域中。下面是我的代码。如何在Mvc3中实现这一点?

这是我的班级

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;
        }       
    }

这是我的控制器。。。。

namespace WriteObjectToDb.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            int result;
            List<person> mn = new List<person>();
            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();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mn"></param>
        /// <returns></returns>
        private int WriteToDb(List<person> 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<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;
            }
        }
    }
}

这是我的看法。。。。。

@model IEnumerable<WriteObjectToDb.Models.person>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            FirstName
        </th>
        <th>
            LastName
        </th>
        <th>
            Address
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}
</table>

将方法的返回值传递给Action Result,Action Result将使用mvc 3 c#在视图的标签中显示

将其存储在ViewBag:中

totalCount = 10;
ViewBag.TotalCount = totalCount;

那么在你看来:

<span>@ViewBag.TotalCount</span>

您可以创建新的视图模型类并向其添加新属性:

视图模型:

public class PersonsViewModel {
    public List<person> Persons { get; set; }
    public Int32 TotalCount{ get; set; }
}

行动:

 public ActionResult Index()
    {
        int result;
        var model = new PersonsViewModel();
        List<person> mn = new List<person>();
        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);
        model.Persons = mn;
        model.TotalCount = 42; // your variable
        return View(model);
    }

视图:

@model PersonsViewModel
@{
    ViewBag.Title = "Index";
}
<h2>Total : @Model.TotalCount</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            FirstName
        </th>
        <th>
            LastName
        </th>
        <th>
            Address
        </th>
        <th></th>
    </tr>
@foreach (var item in Model.Persons) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}
</table>

如果TotalCount只是模型中Person对象的计数,则可以使用model.count.

为了在一般情况下完全回答您的问题:您需要创建一个ViewModel类,该类包含要传递给视图的所有数据,在您的情况下是List People和int TotalCount。将视图模型传递到视图,并访问视图中的属性:

class MyPageNameViewModel
{
    public int TotalCount { get; set; }
    public List<Person> People { get; set; }
}