0


I'm having trouble with complexe form in my back office.
When Form is send to the server in [HTTP POST], my model is wrong. Even if I check boxes, my model always returns false. I hope you guys can help me I'm wasting a lot of time in this problem and found nothing.

Part of my model

<!-- language: lang-csharp -->
public class project{
    ......
    public List<Skill> Skills { get{....} set{}    }
public class Skill
{
    public int SkillId { get; set; }
    public string SkillName { get; set; }
    public string SkillNumber { get; set; }
    public bool IsChecked { get; set; }
    public HttpPostedFileBase files { get; set; }
}

Part of my view

<!-- language: lang-razor -->
for (int i = 0; i < Model.Skills.Count; i++)
{
        <div class="form-group row">
            <div class="col-8">
                @Html.CheckBoxFor(model => model.Skills[i].IsChecked)
                @Html.LabelFor(model => model.Skills[i].IsChecked, Model.Skills[i].SkillName)
            </div>
            <div class="col-4">
                <input type="hidden" name="@Model.Skills[i].SkillId" id="@Model.Skills[i].SkillId"  />
            </div>
        </div>
 }

Part of my controller

<!-- language: lang-csharp -->
foreach (Models.Skill skill in viewModel.Skills)
            {
                if (skill.IsChecked)
                {
                    if(skill.files.ContentLength > 0)
                    {
                        string fileName = skill.SkillNumber;
                        string filepath = (path + "/" + fileName);
                        skill.files.SaveAs(filepath);
                    }
                }
            }

If you guys need more information don't forget to ask.

Updated:

razor - cshtml view

@using (Html.BeginForm())
{
    <div class="form-group">
        @Html.LabelFor(model => model.ProjectTitle)
        @Html.TextBoxFor(model => model.ProjectTitle, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.ProjectTitle, String.Empty, new { @class = "form-text text-muted" })
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.ProjectDescriptionShort)
        @Html.TextBoxFor(model => model.ProjectDescriptionShort, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.ProjectDescriptionShort, String.Empty, new { @class = "form-text text-muted" })
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.ProjectDescriptionFull)
        @Html.TextBoxFor(model => model.ProjectDescriptionFull, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.ProjectDescriptionFull, String.Empty, new { @class = "form-text text-muted" })
    </div>
    <div class="form-group">
        @Html.LabelFor(Model => Model.ProjectTypes)<br />
        @Html.DropDownListFor(Model => Model.ProjectTypeId, Model.ProjectTypes, "Selectionnez une type de projet", new { @class = "form-control" })
        @Html.ValidationMessageFor(Model => Model.ProjectTypes, String.Empty, new { @class = "form-text text-muted" })
    </div>
    for (int i = 0; i < Model.Skills.Count; i++)
    {
            <div class="form-control">
                    @Html.CheckBoxFor(model => model.Skills[i].IsChecked)
                    @Html.LabelFor(model => model.Skills[i].IsChecked, Model.Skills[i].SkillName)
            </div>
        }
    <input type="submit" value="valider" />
}

Controller Action

[HttpPost]
public ActionResult Index(Models.Project viewModel)
{
    if (ModelState.IsValid)
    {
        Models.User user = (Models.User)Session["User"];
        Datas.DataSetProjectTableAdapters.tbProjectTableAdapter tbProjectTableAdapter = new Datas.DataSetProjectTableAdapters.tbProjectTableAdapter();
        tbProjectTableAdapter.Insert(
            viewModel.ProjectTitle
            , viewModel.ProjectDescriptionShort
            , viewModel.ProjectDescriptionFull
            , user.UserId
            , viewModel.ProjectTypeId
            );
        Datas.DataSetProject.tbProjectDataTable lastProjectEntry = tbProjectTableAdapter.GetDataByLastInsert();
        int projectId = (int)lastProjectEntry.Rows[0]["Id"];


        string path = Server.MapPath("~/App_Data/UploadedFiles/Project/" + viewModel.ProjectTitle);  // Give the specific path
        if (!(System.IO.Directory.Exists(path)))
        {
            System.IO.Directory.CreateDirectory(path);
        }
        else{}
        foreach (Models.Skill skill in viewModel.Skills)
        {
            if (skill.IsChecked)
            {
                if(skill.files.ContentLength > 0)
                {
                    ... not implented yet
                }
            }
        }
    }
    return View(viewModel);
}

View Model c#

public class Project
{
    [Required]
    public string ProjectTitle { get; set; }
    [Required]
    public string ProjectDescriptionShort { get; set; }
    [Required]
    public string ProjectDescriptionFull { get; set; }
    [Required]
    public int ProjectTypeId { get; set; }
    public IEnumerable<SelectListItem> ProjectTypes
    {
        get
        {
            Datas.DataSetProject.tbProjectTypeDataTable tbProjectTypes = new Datas.DataSetProjectTableAdapters.tbProjectTypeTableAdapter().GetData();
            List<SelectListItem> Items = new List<SelectListItem>();
            foreach(Datas.DataSetProject.tbProjectTypeRow row in tbProjectTypes)
            {
                Items.Add(new SelectListItem()
                {
                    Value = row.Id.ToString(),
                    Text = row.ProjectName
                });
            }
            return Items;
        }
        set { }
    }

    public List<Skill> Skills
    {
        get
        {
            Datas.DataSetProject.tbSkillDataTable tbSkills = new Datas.DataSetProjectTableAdapters.tbSkillTableAdapter().GetData();
            List<Skill> Items = new List<Skill>();
            foreach (Datas.DataSetProject.tbSkillRow row in tbSkills)
            {
                Items.Add(new Skill()
                {
                    SkillId = row.Id,
                    SkillName = row.SkillNumber + " - " + row.SkillName,
                    SkillNumber = row.SkillNumber

                });
            }
            return Items;
        }
        set { }
    }

}

public class Skill
{
    public int SkillId { get; set; }
    public string SkillName { get; set; }
    public string SkillNumber { get; set; }
    public bool IsChecked { get; set; }
    public HttpPostedFileBase files { get; set; }
}
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
ClementB
  • 3
  • 1

4 Answers4

0

Can you please try below code instead of CheckboxFor, it will work:

@Html.CheckBox("IsChecked", model.Skills[i].IsChecked)

Let me know if you still face an issue after trying this one.

0

you should post all your cshtml file and http post action

razor - cshtml view

@using (Html.BeginForm())
{
    <div class="form-group">
        @Html.LabelFor(model => model.ProjectTitle)
        @Html.TextBoxFor(model => model.ProjectTitle, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.ProjectTitle, String.Empty, new { @class = "form-text text-muted" })
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.ProjectDescriptionShort)
        @Html.TextBoxFor(model => model.ProjectDescriptionShort, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.ProjectDescriptionShort, String.Empty, new { @class = "form-text text-muted" })
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.ProjectDescriptionFull)
        @Html.TextBoxFor(model => model.ProjectDescriptionFull, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.ProjectDescriptionFull, String.Empty, new { @class = "form-text text-muted" })
    </div>
    <div class="form-group">
        @Html.LabelFor(Model => Model.ProjectTypes)<br />
        @Html.DropDownListFor(Model => Model.ProjectTypeId, Model.ProjectTypes, "Selectionnez une type de projet", new { @class = "form-control" })
        @Html.ValidationMessageFor(Model => Model.ProjectTypes, String.Empty, new { @class = "form-text text-muted" })
    </div>
    for (int i = 0; i < Model.Skills.Count; i++)
    {
            <div class="form-control">
                    @Html.CheckBoxFor(model => model.Skills[i].IsChecked)
                    @Html.LabelFor(model => model.Skills[i].IsChecked, Model.Skills[i].SkillName)
            </div>
        }
    <input type="submit" value="valider" />
}

Controller Action

    [HttpPost]
    public ActionResult Index(Models.Project viewModel)
    {
        if (ModelState.IsValid)
        {
            Models.User user = (Models.User)Session["User"];
            Datas.DataSetProjectTableAdapters.tbProjectTableAdapter tbProjectTableAdapter = new Datas.DataSetProjectTableAdapters.tbProjectTableAdapter();
            tbProjectTableAdapter.Insert(
                viewModel.ProjectTitle
                , viewModel.ProjectDescriptionShort
                , viewModel.ProjectDescriptionFull
                , user.UserId
                , viewModel.ProjectTypeId
                );
            Datas.DataSetProject.tbProjectDataTable lastProjectEntry = tbProjectTableAdapter.GetDataByLastInsert();
            int projectId = (int)lastProjectEntry.Rows[0]["Id"];


            string path = Server.MapPath("~/App_Data/UploadedFiles/Project/" + viewModel.ProjectTitle);  // Give the specific path
            if (!(System.IO.Directory.Exists(path)))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            else{}
            foreach (Models.Skill skill in viewModel.Skills)
            {
                if (skill.IsChecked)
                {
                    if(skill.files.ContentLength > 0)
                    {
                        ... not implented yet
                    }
                }
            }
        }
        return View(viewModel);
    }

View Model c#

public class Project
{
    [Required]
    public string ProjectTitle { get; set; }
    [Required]
    public string ProjectDescriptionShort { get; set; }
    [Required]
    public string ProjectDescriptionFull { get; set; }
    [Required]
    public int ProjectTypeId { get; set; }
    public IEnumerable<SelectListItem> ProjectTypes
    {
        get
        {
            Datas.DataSetProject.tbProjectTypeDataTable tbProjectTypes = new Datas.DataSetProjectTableAdapters.tbProjectTypeTableAdapter().GetData();
            List<SelectListItem> Items = new List<SelectListItem>();
            foreach(Datas.DataSetProject.tbProjectTypeRow row in tbProjectTypes)
            {
                Items.Add(new SelectListItem()
                {
                    Value = row.Id.ToString(),
                    Text = row.ProjectName
                });
            }
            return Items;
        }
        set { }
    }

    public List<Skill> Skills
    {
        get
        {
            Datas.DataSetProject.tbSkillDataTable tbSkills = new Datas.DataSetProjectTableAdapters.tbSkillTableAdapter().GetData();
            List<Skill> Items = new List<Skill>();
            foreach (Datas.DataSetProject.tbSkillRow row in tbSkills)
            {
                Items.Add(new Skill()
                {
                    SkillId = row.Id,
                    SkillName = row.SkillNumber + " - " + row.SkillName,
                    SkillNumber = row.SkillNumber

                });
            }
            return Items;
        }
        set { }
    }

}

public class Skill
{
    public int SkillId { get; set; }
    public string SkillName { get; set; }
    public string SkillNumber { get; set; }
    public bool IsChecked { get; set; }
    public HttpPostedFileBase files { get; set; }
}
ClementB
  • 3
  • 1
0

The problem is your view is only displaying a checkbox for each skill and assigning a value to it. It is not binding the checkbox directly to the correct model properties. See this post about creating display templates for a collection. This is the direction you need to take. It's easier to maintain and reusable in the long run.

ASP.net MVC - Display Template for a collection

Also, it looks like your getter of Skills in the view model only ever returns a brand new list. That could be the reason the controller is getting a new list with everything false every time. Try building the list in the constructor of the view model instead and use the default getter and setter.

cwalvoort
  • 1,851
  • 1
  • 18
  • 19
  • Yes, that the point , I try building the list in the controller before sending the viewModel to view, and use default Get/set for my list property and it work !!! thank you so much – ClementB May 05 '19 at 09:34
0

Sometimes CheckboxFor have some anonymous fault, please use below code for as advise.

<input type="hidden" name="@model.Skills[i].SkillId" /> 

<input type="checkbox" name="@model.Skills[i].IsChecked" />
Shahzad Khan
  • 432
  • 2
  • 14