mvc c#使用相关对象列表创建新对象

时间:2022-09-25 10:20:05

Let's say I have a class

比方说我上课了

public class Person 
    {
        public int id { get; set; }
        public string name { get; set; }
        public List<int> list_of_friends { get; set; }
    }

The list of friends contains ids of people that are friends of the selected person. I am having problems creating a form to create a new person. It's supposed to have two fields of id and name and then a checklist of all people to check whether or not the new person is friends with any of them. So far I got this:

朋友列表包含作为所选人的朋友的人的ID。我在创建表单以创建新人时遇到问题。它应该有两个id和name字段,然后是所有人的清单,以检查新人是否与他们中的任何人成为朋友。到目前为止我得到了这个:

My Controller:

 // 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();
            }
        }

(I am supposed to use the Session object - don't be surprised) My view seems to be the problem:

(我应该使用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>

I have no idea how to fill in the ??'s. Can someone help?

我不知道如何填写??。有人可以帮忙吗?

1 个解决方案

#1


0  

Try this bro, hope it helps;

试试这个兄弟,希望它有所帮助;

@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)
}

it should work if i got it right; try to have a look at this tutorial also this might be helpfull for u: Updating Related Data with the Entity Framework in an ASP.NET MVC Application (6 of 10)

如果我做对了它应该有用;试着看一下这个教程,这对你来说可能会有所帮助:在ASP.NET MVC应用程序中使用实体框架更新相关数据(6/10)

#1


0  

Try this bro, hope it helps;

试试这个兄弟,希望它有所帮助;

@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)
}

it should work if i got it right; try to have a look at this tutorial also this might be helpfull for u: Updating Related Data with the Entity Framework in an ASP.NET MVC Application (6 of 10)

如果我做对了它应该有用;试着看一下这个教程,这对你来说可能会有所帮助:在ASP.NET MVC应用程序中使用实体框架更新相关数据(6/10)