0

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:

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:

<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?

Simon
  • 2,643
  • 3
  • 40
  • 61
  • try not to use session. its bad practice for many reasons and overkill for this stuff anyway. you need to use the indexer when you are generating the CheckBoxFor/hiddenfor/labelfor otherwise it will not post back the list correctly. http://stackoverflow.com/questions/20687121/asp-net-mvc-checkboxlist-from-model-with-list-property – Ahmed ilyas Oct 31 '15 at 20:23
  • @Ahmedilyas, what are the reasons using Session is bad practice? – Arturo Torres Sánchez Oct 31 '15 at 21:19
  • You need a view model that represents what you want to display/edit. In your case `PersonVM` which contains a property `List Friends` where `FriendVM` contains properties `int ID`, `string Name` and `bool IsSelected`. Refer [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example –  Oct 31 '15 at 23:37

1 Answers1

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)

moji
  • 267
  • 5
  • 15