Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7928937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:54:00+00:00 2026-06-03T19:54:00+00:00

I have a form, with a partial view inside it to render several child

  • 0

I have a form, with a partial view inside it to render several child controls.

Main view :

@model Test_mvc.Models.Entity.Question
@{
    ViewBag.Title = "Edit";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
    @*snip*@
    <fieldset>
        <legend>Profils</legend>
        @Html.Action("CheckboxList", "Profil", new { id_question = Model.id })
    </fieldset>
    <p>
        <input type="submit" value="Enregistrer" />
    </p>
}

Profil controller (for the partial view) :

    [ChildActionOnly]
    public ActionResult CheckboxList(int id_question)
    {
        var profils = db.Profil.Include("Profil_Question")
            .OrderBy(p => p.nom).ToList();
        ViewBag.id_question = id_question;
        return PartialView(profils);
    }

Profil.CheckBoxList view :

@model List<Test_mvc.Models.Entity.Profil>
@foreach (var p in Model)
{
    <input type="checkbox" name="profil_@(p.id)"
        @if (p.Profil_Question.Where(pc => pc.id_question == ViewBag.id_question).Any())
        {
            @:checked="checked"
        } />
    @Html.Label("profil_" + p.id, p.nom)
    <br />
}

(I don’t want to use @Html.CheckBox because I don’t like being sent “true,false” when the checkbox is checked).

Today, if I want to get the checkboxes that have been checked, I do this, but I think it’s awful :

Question controller (for the main view) :

    [HttpPost]
    public ActionResult Edit(Question question)
    {
        if (ModelState.IsValid)
        {
            db.Question.Attach(question);
            db.ObjectStateManager.ChangeObjectState(question, EntityState.Modified);
            db.SaveChanges();

            // this is what I want to change :
            foreach (string r in Request.Form)
            {
                if (r.StartsWith("profil_") && (Request.Form[r] == "true" || Request.Form[r] == "on")) {
                    var p_q = new Models.Entity.Profil_Question();
                    p_q.id_profil = int.Parse(r.Replace("profil_", ""));
                    p_q.id_question = question.id;
                    db.AddToProfil_Question(p_q);
                }
            }

            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(question);
    }

How would you replace the “foreach” in the last code section ?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:54:02+00:00Added an answer on June 3, 2026 at 7:54 pm

    The first thing I would try would be to give all my checkboxes the same name and put the @id as the value of the box:

    @foreach (var p in Model) {     
         <input type="checkbox" name="profil_checkbox" value="@p.id" 
         @if (p.Profil_Question.Where(pc => pc.id_question == ViewBag.id_question).Any()) 
         {
             @:checked="checked"
         } />  
    @Html.Label("profil_" + p.id, p.nom)     <br /> } 
    

    Then rather than searching for profil_@id I should get an array of results for profile_checkbox which is much easier to work with. I don’t recall exactly how MVC3 processes this, so I can’t guarantee what exactly you’ll get in the postback, but that should be easy enough to check during debugging.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a one form tag inside my Index.aspx view. Index.aspx contains several partial
if i have created a view model and have a partial form that is
I have the following form in a partial view @model PartDetail @using (Ajax.BeginForm(Create, Part,
Inside of an asp.net mvc partial view, I have an Ajax form that posts
In my view, I have a form I want to dynamically render. This form
I am using ASP.Net MVC. I have a partial view which has a form
I have a partial view where I render, if the user has choosen an
On Rails 3.2.3, I have a form in a partial view, using bootstrap 2.0.2
I have a form in a partial view with a required field. I try
i have a partial view with a login form (with username and password fields)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.