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,427
  • 8
  • 36
  • 67
kuntal
  • 1,581
  • 2
  • 16
  • 34
  • Is your code generates on page load.? or on any other action.? – Jithin Raj P R Aug 26 '17 at 11:26
  • is it created automatically? or some code is doing that? – Anant Kumar Singh Aug 26 '17 at 11:26
  • please see I update my question with action which return the partial view – kuntal Aug 26 '17 at 11:30
  • Do not use a `foreach` loop - your code will never bind to your model, you will never get any validation etc - refer [Post an HTML Table to ADO.NET DataTable](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  Aug 26 '17 at 11:32
  • @weBer its generate a dropdown's change event and jquery load the partial. – kuntal Aug 26 '17 at 11:32
  • what i asked is those hidden fields are created automatically? or some code line doing that? – Anant Kumar Singh Aug 26 '17 at 11:32
  • And never under any circumstances, attempt to change the `name` attribute when using `HtmlHelper` methods –  Aug 26 '17 at 11:33
  • You should not change name. If you want to have your own name, just don't use html helpers (Html.CheckboxFor). – LukLed Aug 27 '17 at 12:26

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,427
  • 8
  • 36
  • 67
  • 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