@@ -162,7 +162,10 @@ public async Task<IActionResult> New(NewTrainingModel model, IFormCollection for
162162
163163 if ( ModelState . IsValid )
164164 {
165- List < int > questions = ( from object key in form . Keys where key . ToString ( ) . StartsWith ( "question_" ) select int . Parse ( key . ToString ( ) . Replace ( "question_" , "" ) ) ) . ToList ( ) ;
165+ List < int > questions = form . Keys
166+ . Where ( k => k . StartsWith ( "question_" ) )
167+ . Select ( k => { var s = k . Replace ( "question_" , "" ) ; return int . TryParse ( s , out var v ) ? ( int ? ) v : null ; } )
168+ . Where ( v => v . HasValue ) . Select ( v => v . Value ) . ToList ( ) ;
166169
167170 if ( questions . Count > 0 )
168171 model . Training . Questions = new Collection < TrainingQuestion > ( ) ;
@@ -185,7 +188,11 @@ public async Task<IActionResult> New(NewTrainingModel model, IFormCollection for
185188 var question = new TrainingQuestion ( ) ;
186189 question . Question = questionText ;
187190
188- List < int > answers = ( from object key in form . Keys where key . ToString ( ) . StartsWith ( "answerForQuestion_" + i + "_" ) select int . Parse ( key . ToString ( ) . Replace ( "answerForQuestion_" + i + "_" , "" ) ) ) . ToList ( ) ;
191+ var answerPrefix1 = "answerForQuestion_" + i + "_" ;
192+ List < int > answers = form . Keys
193+ . Where ( k => k . StartsWith ( answerPrefix1 ) )
194+ . Select ( k => { var s = k . Replace ( answerPrefix1 , "" ) ; return int . TryParse ( s , out var v ) ? ( int ? ) v : null ; } )
195+ . Where ( v => v . HasValue ) . Select ( v => v . Value ) . ToList ( ) ;
189196
190197 if ( answers . Count > 0 )
191198 question . Answers = new Collection < TrainingQuestionAnswer > ( ) ;
@@ -366,9 +373,9 @@ public async Task<IActionResult> Edit(int trainingId, EditTrainingModel model, I
366373
367374 foreach ( var group in groups )
368375 {
369- if ( ! string . IsNullOrWhiteSpace ( group ) )
376+ if ( ! string . IsNullOrWhiteSpace ( group ) && int . TryParse ( group , out var groupId ) )
370377 {
371- var members = await _departmentGroupsService . GetAllMembersForGroupAsync ( int . Parse ( group ) ) ;
378+ var members = await _departmentGroupsService . GetAllMembersForGroupAsync ( groupId ) ;
372379
373380 foreach ( var member in members )
374381 {
@@ -387,9 +394,9 @@ public async Task<IActionResult> Edit(int trainingId, EditTrainingModel model, I
387394
388395 foreach ( var role in roles )
389396 {
390- if ( ! string . IsNullOrWhiteSpace ( role ) )
397+ if ( ! string . IsNullOrWhiteSpace ( role ) && int . TryParse ( role , out var roleId ) )
391398 {
392- var roleMembers = await _personnelRolesService . GetAllMembersOfRoleAsync ( int . Parse ( role ) ) ;
399+ var roleMembers = await _personnelRolesService . GetAllMembersOfRoleAsync ( roleId ) ;
393400
394401 foreach ( var member in roleMembers )
395402 {
@@ -425,7 +432,10 @@ public async Task<IActionResult> Edit(int trainingId, EditTrainingModel model, I
425432 else
426433 existingTraining . Questions . Clear ( ) ;
427434
428- List < int > questions = ( from object key in form . Keys where key . ToString ( ) . StartsWith ( "question_" ) select int . Parse ( key . ToString ( ) . Replace ( "question_" , "" ) ) ) . ToList ( ) ;
435+ List < int > questions = form . Keys
436+ . Where ( k => k . StartsWith ( "question_" ) )
437+ . Select ( k => { var s = k . Replace ( "question_" , "" ) ; return int . TryParse ( s , out var v ) ? ( int ? ) v : null ; } )
438+ . Where ( v => v . HasValue ) . Select ( v => v . Value ) . ToList ( ) ;
429439
430440 foreach ( var i in questions )
431441 {
@@ -436,7 +446,11 @@ public async Task<IActionResult> Edit(int trainingId, EditTrainingModel model, I
436446 question . Question = questionText ;
437447 question . TrainingId = trainingId ;
438448
439- List < int > answers = ( from object key in form . Keys where key . ToString ( ) . StartsWith ( "answerForQuestion_" + i + "_" ) select int . Parse ( key . ToString ( ) . Replace ( "answerForQuestion_" + i + "_" , "" ) ) ) . ToList ( ) ;
449+ var answerPrefix = "answerForQuestion_" + i + "_" ;
450+ List < int > answers = form . Keys
451+ . Where ( k => k . StartsWith ( answerPrefix ) )
452+ . Select ( k => { var s = k . Replace ( answerPrefix , "" ) ; return int . TryParse ( s , out var v ) ? ( int ? ) v : null ; } )
453+ . Where ( v => v . HasValue ) . Select ( v => v . Value ) . ToList ( ) ;
440454
441455 if ( answers . Count > 0 )
442456 question . Answers = new Collection < TrainingQuestionAnswer > ( ) ;
@@ -510,7 +524,10 @@ public async Task<IActionResult> Quiz(ViewTrainingModel model, IFormCollection f
510524 if ( training == null )
511525 return NotFound ( ) ;
512526
513- List < int > questions = ( from object key in form . Keys where key . ToString ( ) . StartsWith ( "question_" ) select int . Parse ( key . ToString ( ) . Replace ( "question_" , "" ) ) ) . ToList ( ) ;
527+ List < int > questions = form . Keys
528+ . Where ( k => k . StartsWith ( "question_" ) )
529+ . Select ( k => { var s = k . Replace ( "question_" , "" ) ; return int . TryParse ( s , out var v ) ? ( int ? ) v : null ; } )
530+ . Where ( v => v . HasValue ) . Select ( v => v . Value ) . ToList ( ) ;
514531
515532 foreach ( var questionId in questions )
516533 {
0 commit comments