一个方法的返回值传递给一个操作结果是要被显示在一个标签上使用MVC 3 C#视图 [英] Passing 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#
本文介绍了一个方法的返回值传递给一个操作结果是要被显示在一个标签上使用MVC 3 C#视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我会想变量TOTALCOUNT传递给我的看法,并让这
显示标签或文本区域时,我的看法负荷。这里是我的低于code。
我怎样才能在MVC 3实现这一目标?
下面是我的班
公共类人
{ 公众诠释标识{搞定;组; }
公共字符串名字{获得;组; }
公共字符串名字{获得;组; }
公共字符串地址{搞定;组; } 公众人物(INT MYID,串myFirstname,串myLastname,串myAddress)
{
ID =本身份识别码;
名字= myFirstname;
姓氏= myLastname;
地址= myAddress;
}
}
下面是我的控制器....
命名空间WriteObjectToDb.Controllers
{
公共类HomeController的:控制器
{
公众的ActionResult指数()
{
INT结果;
清单<&人GT; MN =新的List<&人GT;();
mn.Add(新人(1,约翰,阿德巴约,shobanjo接近杰里科));
mn.Add(新的人(2,国宝,强生,ibada接近));
mn.Add(新人(3,巴约,olomale,oluyole房地产环城公路));
结果= WriteToDb(MN); 返回查看(MN); } 公众的ActionResult关于()
{
返回查看();
}
///<总结>
///
///< /总结>
///< PARAM NAME =MN>< /参数>
///<&回报GT;< /回报>
私人诠释WriteToDb(列表<&人GT; MN)
{
INT TOTALCOUNT;
字符串连接=的String.Empty;
使用(TransactionScope的范围=新的TransactionScope())
{
字符串sqlIns =插入到表(名字,姓氏,地址)的值(@名字,姓氏@,@地址);
SqlConnection的CN =新的SqlConnection(连接);
的SqlCommand cmdIns =新的SqlCommand(sqlIns,CN); 的for(int i = 0; I< mn.Count;我++)
{
cmdIns.Parameters.AddWithValue(@名字,MN [I] .FirstName);
cmdIns.Parameters.AddWithValue(@姓氏,MN [I] .LastName);
cmdIns.Parameters.AddWithValue(@地址,MN [I]。地址);
cmdIns.ExecuteNonQuery();
cmdIns.Parameters.Clear();
}
scope.Complete();
TOTALCOUNT = mn.Count();
返回TOTALCOUNT;
} } }
}
下面是我的看法......
@model IEnumerable的< WriteObjectToDb.Models.person>@ {
ViewBag.Title =指数;
}< H2>指数< / H>&所述p为H.;
@ Html.ActionLink(新建,创建)
&所述; / P>
<表>
&所述; TR>
<第i个
名字
< /第i
<第i个
姓
< /第i
<第i个
地址
< /第i
百分位>< /第i
< / TR>@foreach(以型号VAR项){
&所述; 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(编辑,编辑,新{ID = item.Id})|
@ Html.ActionLink(详细信息,详细信息,新{ID = item.Id})|
@ Html.ActionLink(删除,删除,新{ID = item.Id})
< / TD>
< / TR>
}< /表>
解决方案
您可以创建新的ViewModel类和新属性添加到它:
查看模式:
公共类PersonsViewModel {
公开名单<&人GT;人{搞定;组; }
公众的Int32 TOTALCOUNT {搞定;组; }
}
动作:
公众的ActionResult指数()
{
INT结果;
VAR模型=新PersonsViewModel();
清单<&人GT; MN =新的List<&人GT;();
mn.Add(新人(1,约翰,阿德巴约,shobanjo接近杰里科));
mn.Add(新的人(2,国宝,强生,ibada接近));
mn.Add(新人(3,巴约,olomale,oluyole房地产环城公路));
结果= WriteToDb(MN);
model.Persons = MN;
model.TotalCount = 42; //你的变量 返回查看(模型); }
查看:
@model PersonsViewModel@ {
ViewBag.Title =指数;
}< H2>总计:@ Model.TotalCount< / H>&所述p为H.;
@ Html.ActionLink(新建,创建)
&所述; / P>
<表>
&所述; TR>
<第i个
名字
< /第i
<第i个
姓
< /第i
<第i个
地址
< /第i
百分位>< /第i
< / TR>@foreach(在Model.Persons VAR项){
&所述; 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(编辑,编辑,新{ID = item.Id})|
@ Html.ActionLink(详细信息,详细信息,新{ID = item.Id})|
@ Html.ActionLink(删除,删除,新{ID = item.Id})
< / TD>
< / TR>
}
< /表>
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?
Here is my class
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;
}
}
Here is my controller ....
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;
}
}
}
}
Here is my view.....
@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>
解决方案
You can create new viewmodel class and add new property to it:
View model:
public class PersonsViewModel {
public List<person> Persons { get; set; }
public Int32 TotalCount{ get; set; }
}
Action:
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);
}
View:
@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>
这篇关于一个方法的返回值传递给一个操作结果是要被显示在一个标签上使用MVC 3 C#视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文