[英]Choose One Model in List<t> from view and send back to action
As we know we can pass data from controller to views using ViewData
, ViewBag
, TempData
, Session
And Model
. 众所周知,我们可以使用ViewData
, ViewBag
, TempData
, Session
和Model
将数据从控制器传递到视图。 I used model to send list of dynamically created List object to View. 我使用模型将动态创建的List对象的列表发送到View。
Model : 型号:
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string family { get; set; }
public string Link { get; set; }
}
Controller : 控制器:
List<Person> lstPerson = new List<Person>();
lstPerson.Add(new Person() { ID = 1, Name = "n1", Link = "l1" });
lstPerson.Add(new Person() { ID = 2, Name = "n2", Link = "l2" });
lstPerson.Add(new Person() { ID = 3, Name = "n3", Link = "l3" });
lstPerson.Add(new Person() { ID = 4, Name = "n4", Link = "l4" });
return View(lstPerson);
Fill DropDownList
by binding to Model List
. 通过绑定到Model List
来填充DropDownList
。
@model List<MvcApplication2.Models.Person>
<h2>Index</h2>
@Html.BeginForm("Send Model"){
@Html.DropDownList(
"Foo",
new SelectList(
Model.Select(x => new { Value = x.ID, Text = x.Name }),
"Value",
"Text"
)
)
<input type="submit" value="Send" />
}
Sending back List of model is possible using @Html.ActionLink("Index","Index",lstPerson)
, but how do we know which item is selected? 使用@Html.ActionLink("Index","Index",lstPerson)
可以发送回模型列表,但是我们如何知道选择了哪个项目呢? What is the best way to catch model that's selected in dropdownlist and send back to controller and work with the selected model in list? 捕获在下拉列表中选择的模型并将其发送回控制器并与列表中的选定模型一起使用的最佳方法是什么?
Model: 模型:
public class PeopleViewModel
{
public List<SelectListItem> People {get;set;}
public int SelectedPersonId {get;set;}
}
Controller: 控制器:
public ActionResult People()
{
List<Person> lstPerson = new List<Person>();
lstPerson.Add(new Person() { ID = 1, Name = "n1", Link = "l1" });
lstPerson.Add(new Person() { ID = 2, Name = "n2", Link = "l2" });
lstPerson.Add(new Person() { ID = 3, Name = "n3", Link = "l3" });
lstPerson.Add(new Person() { ID = 4, Name = "n4", Link = "l4" });
TempData["PeopleList"] = lstPerson;
var model = new PeopleViewModel
{
People = lstPerson.Select(
p => new SelectListItem{ Value = p.ID.ToString(), Text = p.Name}).ToList()
}
return View(model);
}
View: 视图:
@model PeopleViewModel
<h2>Index</h2>
@Html.BeginForm("Send Model"){
@Html.DropDownListFor(m=>m.SelectedPersonId,Model.People)
)
<input type="submit" value="Send" />
}
Controller post action 控制器后动作
[HttpPost]
public ActionResult People(PeopleViewModel model)
{
var peopleList = (List<Person>)TempData["PeopleList"];
var selectedPerson = peopleList.FirstOrDefault(p=>p.ID == model.SelectedPersonId);
}
Send Back List Of Model Is Possible to using @Html.ActionLink("Index","Index",lstPerson). 使用@ Html.ActionLink(“ Index”,“ Index”,lstPerson)可以发送回模型列表。
Actually this is wrong assumption: 实际上这是错误的假设:
Html.ActionLink
simply generates a link : <a href="">
Html.ActionLink
只是生成一个链接: <a href="">
<input>
or <select>
仅将输入发布回控制器,因此如果要发布内容,则应将其呈现为<input>
或<select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.