Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将方法的返回值传递给操作结果,该操作结果将使用MVC3C显示在视图的标签中#_C#_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

C# 将方法的返回值传递给操作结果,该操作结果将使用MVC3C显示在视图的标签中#

C# 将方法的返回值传递给操作结果,该操作结果将使用MVC3C显示在视图的标签中#,c#,asp.net-mvc,asp.net-mvc-3,C#,Asp.net Mvc,Asp.net Mvc 3,我想把变量“totalCount”传递给我的视图,让它 加载“我的视图”时显示在标签或文本区域中。下面是我的代码。 如何在MVC3中实现这一点 这是我的班级 public class person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public st

我想把变量“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;
            }

        }

    }
}
namespace WriteObjectToDb.Controllers
{
公共类HomeController:控制器
{
公共行动结果索引()
{
int结果;
List mn=新列表();
mn.Add(新人物(1,“约翰”、“阿德巴约”、“肖班乔-杰里科”);
mn.添加(新人物(2,“大卫”、“约翰逊”、“ibada close”);
mn.Add(新增人员(3名,“bayo”、“olomale”、“oluyole estate ringroad”);
结果=WriteToDb(mn);
返回视图(mn);
}
关于()的公共行动结果
{
返回视图();
}
/// 
/// 
/// 
/// 
/// 
私有int WriteToDb(列表mn)
{
整数总数;
string connect=string.Empty;
使用(TransactionScope范围=新TransactionScope())
{
string sqlIns=“插入表(firstname、lastname、address)值(@firstname、@lastname、@address)”;
SqlConnection cn=新的SqlConnection(connect);
SqlCommand cmdIns=新的SqlCommand(sqlIns,cn);
for(int i=0;i item.FirstName)
@DisplayFor(modelItem=>item.LastName)
@DisplayFor(modelItem=>item.Address)
@ActionLink(“编辑”,“编辑”,新的{id=item.id})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.id})|
@ActionLink(“删除”,“删除”,新的{id=item.id})
}
将其存储在:

那么在你看来,

<span>@ViewBag.TotalCount</span>
@ViewBag.TotalCount

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

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

class MyPageNameViewModel
{
    public int TotalCount { get; set; }

    public List<Person> People { get; set; }

}
类MyPageNameViewModel
{
公共整数TotalCount{get;set;}
公共列表人员{get;set;}
}

您可以创建新的viewmodel类并向其添加新属性:

视图模型:

public class PersonsViewModel {
    public List<person> Persons { get; set; }
    public Int32 TotalCount{ get; set; }
}
公共类PersonView模型{
公共列表人员{get;set;}
公共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);

    }
public ActionResult Index()
{
int结果;
var模型=新PersonViewModel();
List mn=新列表();
mn.Add(新人物(1,“约翰”、“阿德巴约”、“肖班乔-杰里科”);
mn.添加(新人物(2,“大卫”、“约翰逊”、“ibada close”);
mn.Add(新增人员(3名,“bayo”、“olomale”、“oluyole estate ringroad”);
结果=WriteToDb(mn);
模型人=mn;
model.TotalCount=42;//您的变量
返回视图(模型);
}
视图:

@model PersonsViewModel
@{
ViewBag.Title=“Index”;
}
总计:@Model.TotalCount

@ActionLink(“新建”、“创建”)

名字 姓氏 地址 @foreach(Model.Persons中的var项目){ @DisplayFor(modelItem=>item.FirstName) @DisplayFor(modelItem=>item.LastName) @DisplayFor(modelItem=>item.Address) @ActionLink(“编辑”,“编辑”,新的{id=item.id})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.id})| @ActionLink(“删除”,“删除”,新的{id=item.id}) }
谢谢。我喜欢这种方法。我现在如何将TotalCount的值传递给视图或文本区域上的标签。我希望显示计数。@Html.DisplayFor(model=>model.TotalCount)
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>