0

I have a partial view like bellow

@model IEnumerable<elearnerhub.Common.Entities.ViewModel.elearnerhub.vm_eLearning_Master_QuestionSet_AnswerOptions>

    @foreach (var item in Model)
    {
        <div class="form-group">
            @Html.LabelFor(m => item.AnswerShownOrder,item.AnswerShownOrder.ToString(), new { @class = "control-label col-md-3 col-sm-3 col-xs-12" })
            <div class="col-md-4 col-sm-4 col-xs-4">
                @Html.TextBoxFor(m => item.AnswerOptionText, new { @class = "form-control", @id=item.PK_MasterQuestion_AnswerOptionID, Name = "AnswerOptionText" })


            </div>
            <div class="col-md-2 col-sm-2 col-xs-2">

                @Html.CheckBoxFor(m => item.CorrectAnswer, new { @class = "form-control", @id = "chk"+item.PK_MasterQuestion_AnswerOptionID, @Name = "CorrectAnswer" })

            </div>
        </div>
    }

public PartialViewResult _QuestionAnswerOptions(Int64 noofoptions)
    {

        List<vm_eLearning_Master_QuestionSet_AnswerOptions> _vm_eLearning_Master_QuestionSet_AnswerOptions = new List<vm_eLearning_Master_QuestionSet_AnswerOptions>();


        for(int i = 0; i <= noofoptions-1; i++)
        {
            var obj = new vm_eLearning_Master_QuestionSet_AnswerOptions
            {
                PK_MasterQuestion_AnswerOptionID= i + 1,
                AnswerShownOrder=i+1,
                AnswerOptionText="",
                CorrectAnswer=false


            };
            _vm_eLearning_Master_QuestionSet_AnswerOptions.Add(obj);
        }

        return PartialView("_QuestionSet_AnswerOptions");
    }

and the hidden field generate with checkbox with different name .please see the screen shot of inspect element

enter image description here

How to set the name of the hidden field same as check box?

Jithin Raj P R
  • 6,667
  • 8
  • 38
  • 69
kuntal
  • 1,591
  • 2
  • 16
  • 36

1 Answers1

0

You can use this simple snippet as your solution -

Here the .dropDownClass should be the class you used for your dropdown

$('body').on('change', '.dropDownClass', function() {
  var naMe = $('#chk1').attr('name')
  $('#chk1').parent().find('input[type="hidden"]').attr('name', naMe)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select name="" id="" class="dropDownClass">
 <option value="1">1</option>
 <option value="2">2</option>
</select>

<div class="col-md-2">
  <input id="chk1" class="form-control" name="CorrectAnswer" data-value="true" value="true" type="checkbox">
  <input name="item.CorrectAnswer" value="false" type="hidden">
</div>

I hope this helps you, if there is anything more please do ask.

Jithin Raj P R
  • 6,667
  • 8
  • 38
  • 69
  • thanks for your code @weBer but I don't want extra code for fixing ,Is the no way to fix this with MVC please see my post I update my code there – kuntal Aug 26 '17 at 11:41
  • If it's like that then you should not have put `jQuery` tag. Sorry, i can only help you with jQuery. – Jithin Raj P R Aug 26 '17 at 11:43
  • Is there any MVC option where we can control the hidden field generate with checkboxfor? – kuntal Aug 26 '17 at 11:44
  • Am not used to do the project with **MVC** so can't help you on that. but this is a simple jQuery solution you can use. – Jithin Raj P R Aug 26 '17 at 11:46