rahul patil

rahul patil

  • NA
  • 160
  • 8.7k

I want to insert multiple hobby in database?

Jun 21 2020 9:45 AM
when student select multiple hobbies then I want to insert them in table
 
database fieldname
 
studenthobbies bit
 
student.cs
  1. public Nullable<bool> studenthobbies { getset; }    
HomeController.cs
  1. [HttpPost]    
  2. public ActionResult Create(student student, List<student> hobbies)    
  3. {    
  4.         //foreach (student hobby in hobbies)    
  5.         //{    
  6.         //    student updatedHobby = InsAjaxEntities.students.ToList().Find(p => p.studenthobbies == hobby.studenthobbies);    
  7.         //}    
  8.    
  9.         en.students.Add(student);    
  10.         en.SaveChanges();    
  11.         return RedirectToAction("Index");    
  12.  }  
Create.cshtml
  1.   <div class="row">      
  2.            <div class="form-group col-lg-10">      
  3.                <label class="control-label col-lg-2"><strong>StudentHobby:</strong></label>      
  4.   
  5.                <input type="checkbox" id="wallyball" name="wallyball" value="wallyball" /> wallyball    
  6.                <input type="checkbox" id="swimming" name="swimming" value="swimming" checked /> swimming    
  7.                <input type="checkbox" id="News" name="News" value="News" /> News    
  8.                      
  9.            </div>      
  10.        </div>      
  11.        <button id="button" type="button" value="submit" class="btn-primary">Save</button>    
  12.        
  13.        @section scripts {    
  14. <div>    
  15.     <script src="~/Scripts/jquery.validate.min.js"></script>    
  16.     <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>    
  17.   
  18.     <script type="text/javascript">    
  19.   
  20.             $('#button').click(function () {    
  21.                 debugger    
  22.   
  23.                 $.ajax({    
  24.                     type: 'POST',    
  25.                     url: '/Home/Create',    
  26.                     data: {    
  27.                   
  28.                         //wallyball: $('#wallyball').val(),    
  29.                         //swimming: $('#swimming').val(),    
  30.                         //News: $('#News').val(),    
  31.   
  32.                     },    
  33.                     success: function (data) {    
  34.                         debugger    
  35.                         alert('success', data);    
  36.                     },    
  37.                     error: function (x, y, z) {    
  38.   
  39.                         alert(x);    
  40.                         alert(y);    
  41.                         alert(z);    
  42.                         debugger    
  43.                     }    
  44.                 });    
  45.             });    
  46.     </script>    
  47. </div>  
I am using the ajax code for insert the data how to insert multiple hobby in table?
 
I want to insert multiple hobby in table?
 
help?

Answers (2)