[RESOLVED]Issues with IsPost-

THis form is meant to get the list of checkboxes (multiple) being checked and iterate it into the Subscribed table in the database.. The form keeps refreshing after excessive debug, I turned here for solution.

Find below the form 

if(IsPost){
 
// Add each of your values into a List of they exist
List<string> selectedCategories = new List<string>();
 
// Check if each one exists and if so add it
if(Request["Art"] != null) { selectedCategories.Add(Request["Art"]); }
if(Request["Business"] != null) { selectedCategories.Add(Request["Business"]); }
if(Request["Careers"] != null) { selectedCategories.Add(Request["Careers"]); }
if(Request["Education"] != null) { selectedCategories.Add(Request["Education"]); }
if(Request["Entertainment"] != null) { selectedCategories.Add(Request["Entertainment"]); }
if(Request["Fashion"] != null) { selectedCategories.Add(Request["Fashion"]); }
if(Request["Food"] != null) { selectedCategories.Add(Request["Food"]); }
if(Request["Medicine"] != null) { selectedCategories.Add(Request["Medicine"]); }
if(Request["Judiciary"] != null) { selectedCategories.Add(Request["Judiciary"]); }
if(Request["Lifestyle"] != null) { selectedCategories.Add(Request["Lifestyle"]); }
if(Request["Politics"] != null) { selectedCategories.Add(Request["Politics"]); }
if(Request["Religion"] != null) { selectedCategories.Add(Request["Religion"]); }
if(Request["Science"] != null) { selectedCategories.Add(Request["Science"]); }
if(Request["Social"] != null) { selectedCategories.Add(Request["Social"]); }
if(Request["Sport"] != null) { selectedCategories.Add(Request["Sport"]); }
if(Request["Technology"] != null) { selectedCategories.Add(Request["Technology"]); }
 
 
// For each value in your collection, add it
foreach(var category in selectedCategories)
{
    db.Execute("INSERT INTO Subscribed(userID, catID) VALUES (@0, @1)",WebSecurity.GetUserId(WebSecurity.CurrentUserName),Convert.ToInt32(category));
}
 var sqlUpdate= "UPDATE UserProfile SET IsComplete = @0 WHERE UserID = @1";
    db.Execute(sqlUpdate, true,WebSecurity.GetUserId(WebSecurity.CurrentUserName));
   var message = "Congratulations on your registeration. We look forward to your stories.";
                   Session["message"] = message;
 
 
//Response.Redirect("~/Art and Design.cshtml");
}


<form action="" method="post">  
                    <div class="panel-body" style=" overflow-y: scroll;
    height: 370px;">
     <div class="row">
                 <span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">
      <img src="~/img/v/art.png" alt="X" class="pull-left"><h2 class="pull-right">Art & Design</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Art"]" value="1">
    </span>
  <span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="info">
      <img src="~/img/v/business.png" alt="X" class="pull-left"><h2 class="pull-right">Business</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Business"]" value="2">
    </span>
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">
      <img src="~/img/v/career.png" alt="X" class="pull-left"><h2 class="pull-right">Careers & Work</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Careers"]" value="3">
    </span>
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="info">
      <img src="~/img/v/education.png" alt="X" class="pull-left"><h2 class="pull-right">Education</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Education"]" value="4">
    </span>
        
              <span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">
      <img src="~/img/v/entertainment.png" alt="X" class="pull-left"><h2 class="pull-right">Entertainment</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Entertainment"]" value="5">
    </span>
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="info">
      <img src="~/img/v/fashion.png" alt="X" class="pull-left"><h2 class="pull-right">Fashion</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Fashion"]" value="6">
    </span>
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">
      <img src="~/img/v/food.png" alt="X" class="pull-left"><h2 class="pull-right">Food & Drink</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Food"]" value="7">
    </span>
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="info">
      <img src="~/img/v/health.png" alt="X" class="pull-left"><h2 class="pull-right">Medicine</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Medicine"]" value="8">
    </span>
 
                <span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">
      <img src="~/img/v/law.png" alt="X" class="pull-left"><h2 class="pull-right">Judiciary</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Judiciary"]" value="9">
    </span>
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="info">
      <img src="~/img/v/lifestyle.png" alt="X" class="pull-left"><h2 class="pull-right">Lifestyle</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Lifestyle"]" value="10">
    </span>
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">
      <img src="~/img/v/politics.png" alt="X" class="pull-left"><h2 class="pull-right">Politics</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Politics"]" value="11">
    </span>
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="info">
      <img src="~/img/v/religion.png" alt="X" class="pull-left"><h2 class="pull-right">Religion</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Religion"]" value="12">
    </span>
                
      <span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">
      <img src="~/img/v/science.png" alt="X" class="pull-left"><h2 class="pull-right">Science</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Science"]" value="13">
    </span>
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="info">
      <img src="~/img/v/media.png" alt="X" class="pull-left"><h2 class="pull-right">Social Media</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Social"]" value="14">
    </span>
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">
      <img src="~/img/v/sport.png" alt="X" class="pull-left"><h2 class="pull-right">Sports</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Sport"]" value="15">
    </span>
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="info">
      <img src="~/img/v/tech.png" alt="X" class="pull-left"><h2 class="pull-right">Technology</h2>
        </button>
        <input type="checkbox" class="hidden" name="@Request["Technology"]" value="16">
    </span>
              
              </div></div>
   <div class="panel-footer" style="background-color: #FFFFFF;">
             <input name="completereg" type="submit" value="Continue" class="btn btn-primary" > 
                    
                </div>
            </form>

What do you mean by keeps refreshing? Does it automatically refresh after loading? Because the only redirect found is an infinite loop which is automatically stopped by the browser. 

Secondly, you know that the Request.Form is a collection, so why not just simply iterate over it? 

http://msdn.microsoft.com/en-us/library/ms525985(v=vs.90).aspx

foreach (var item in Request.Form) {
   if(item != null) {
     // add it to the list, 
   }
}

Shorter code is better code! Wink

Hello,

I cannot understand the exact question, but in ALL checkboxes why did you write 

name="@Request["Art"]"

Just change them

name="Art"

and the others also..

Leave a Reply