mvc#使用相关对象列表创建新对象
假设我有一节课mvc#使用相关对象列表创建新对象,c#,asp.net-mvc,C#,Asp.net Mvc,假设我有一节课 public class Person { public int id { get; set; } public string name { get; set; } public List<int> list_of_friends { get; set; } } 公共类人物 { 公共int id{get;set;} 公共字符串名称{get;set;} 公共列表朋友的列表{get;set;} } 朋友
public class Person
{
public int id { get; set; }
public string name { get; set; }
public List<int> list_of_friends { get; set; }
}
公共类人物
{
公共int id{get;set;}
公共字符串名称{get;set;}
公共列表朋友的列表{get;set;}
}
朋友列表包含所选人员的朋友ID。
我在创建表单以创建一个新人时遇到问题。它应该有两个id和name字段,然后是所有人的清单,以检查新朋友是否与他们中的任何人是朋友。到目前为止,我得到了这个:
我的控制器:
// GET: Person/Create
public ActionResult Create()
{
var model = new Person();
return View(model);
}
// POST: Person/Create
[HttpPost]
public ActionResult Create(Person person)
{
person.id = new Random().Next();
try
{
var list = (List<Person>)Session["list_of_people"];
if (list != null)
{
list.Add(person);
}
else
{
list = new List<Person>();
list.Add(person);
}
Session["list_of_people"] = list;
return RedirectToAction("List");
}
catch
{
return View();
}
}
//GET:Person/Create
公共操作结果创建()
{
var模型=新人();
返回视图(模型);
}
//职位:个人/创建
[HttpPost]
公共行动结果创建(个人)
{
person.id=new Random().Next();
尝试
{
变量列表=(列表)会话[“列表人”];
如果(列表!=null)
{
列表。添加(人);
}
其他的
{
列表=新列表();
列表。添加(人);
}
会话[“人员列表”]=列表;
返回重定向到操作(“列表”);
}
接住
{
返回视图();
}
}
(我应该使用Session对象-不要惊讶)
我的观点似乎是问题所在:
<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@if (Session["list_of_people"] != null)
{
List<Person> list = (List<Person>)Session["list_of_people"];
<p class="lista">List of friends</p>
@for (int i = 0; i < list.Count; i++)
{
@Html.CheckBoxFor(m => ??)
@Html.HiddenFor(m => ??)
@Html.LabelFor(m => ??)
<br />
}
}
</div>
人
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.name,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.name,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.name,“,new{@class=“text danger”})
@if(会话[“人员列表”!=null)
{
列表=(列表)会话[“列表人”];
朋友名单
@for(int i=0;i??)
@Html.HiddenFor(m=>??)
@Html.LabelFor(m=>??)
}
}
我不知道如何填写。有人能帮忙吗 试试这个兄弟,希望对你有帮助
@for (var i =0; i< Person.list_of_friends.Count(); i++){
@Html.HiddenFor(model => model.list_of_friends[i])
@Html.CheckBoxFor(model => model.list_of_friends[i].Selected)
@Html.LabelFor(model=> model.list_of_friends[i].name)
}
@for(var i=0;imodel.list\u朋友[i])
@Html.CheckBoxFor(model=>model.list\u朋友[i]。选中)
@Html.LabelFor(model=>model.list\u朋友[i].name)
}
如果我做对了,它应该会起作用;
试着看一下本教程,这可能对你有帮助:
尽量不要使用会话。出于许多原因,这是一种不好的做法,但无论如何,这种做法也太过分了。生成/hiddenfor/labelf复选框时需要使用索引器,否则它将无法正确发回列表@Ahmedilyas,使用Session不好的原因是什么?您需要一个视图模型来表示您要显示/编辑的内容。在您的案例中,
PersonVM
包含属性列出好友
,其中FriendVM
包含属性int-ID
,字符串名
和bool被选中
。请参阅示例