Pass group of checks from a view to a contractor as a list or as a list, using a complex model c #

0

Greetings The issue is that I have a complex model which I am invoking data from 2 normal models to take it to the screen and create a screen with multiple check with the intention of saving them using an ActionResult

Here is the complex model that inherits from other models

namespace SeguridadWeb.Models
{
    public class ProfileExtendTypeComplex
    {
        public ICollection<Type> TypeProfileExtend { get; set; }
        public ICollection<ProfileExtended> ProfileExt { get; set; }
    }
}

when it is seen, a table is filled using the following line of code

@foreach (var item in Model.TypeProfileExtend)
                    {
                        List<ProfileExtended> permiso = (from itemProfileExt in Model.ProfileExt
                                                         where itemProfileExt.idProfileExtendedType == item.idType
                                                         select itemProfileExt).ToList();

                        <tr>
                            <td style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">
                                @if (permiso.Count > 0)
                                {
                                    @Html.Hidden("hdidProfileExtended_" + item.idType.ToString(), permiso[0].idProfileExtended)
                                }
                                @Html.Label("label_" + item.idType.ToString(), item.nameType, htmlAttributes: new { @class = "control-label" })

                            </td>
                            <td>
                                @Html.CheckBox("chkProfileExt_" + item.idType.ToString(), permiso.Count > 0 ? Convert.ToBoolean(permiso[0].value) : false, new { @id = "chkProfileExt_" + item.idType.ToString() })

                            </td>

                        </tr>
                    }

with the send button to the controller to perform the data insertion

<input type="submit" value="Crear" class="btn btn-primary pull-right" />

the view is in this way at html level

The problem is when it comes to saving the information I'm getting to the controller as null

this is the controller I'm using

// POST: ProfileViewModel/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([[Bind(Include = "idProfileExtended,idProfileExtendedType,value")] ProfileExtended ProfileExtended)
        {
            try
            {

                if (ModelState.IsValid)
                {
                    db.ProfileExtended.Add(ProfileExtended);
                    db.SaveChanges();

                       return RedirectToAction("Index");
                    //}
                }


                return RedirectToAction("ProfileComplex/CreateEdit");
            }
            catch
            {
                return View();
            }
        } 

Any ideas of what could happen or what am I doing wrong?

Thanking you in advance for your help.

    
asked by victor silgado 14.11.2017 в 21:18
source

2 answers

0

In a list it is better to use for cycles instead of foreach.

For example:

@for (int i = 0; i < Model.Count(); i++)
{
    @Html.HiddenFor(m => m[i].idGuess)
}

Use it specifically according to your requirement. If you have specific questions, do not hesitate to comment and we will gladly check it out.

    
answered by 14.11.2017 в 22:05
0

Greetings, after reading a lot I found this site that helped me find the answer, basically when you use complex models you must indicate what is the table and the values to which you are referring for the mvc to understand.

for example, if you previously used idProfilePage, you should call it now as ProfilePage.idPagePage

LKes I leave the link of the link link

    
answered by 23.11.2017 в 16:36