k80sg
k80sg

Reputation: 2473

MVC HTML@Beginform + Submitting a form with existing hiddenfield value from Layout view

In my share _Layout.cshtml view, I have a hiddenfield which I want to persist a value for use in multiple pages. Now in one of the page, I have to submit a form which requires that particular hiddenfield value. I was thinking all I need is just to pass in the hiddenfield value from my _Layout.cshtml but wasn't sure how to go about assigning it, the below works but I am creating an additional hiddenfield and assign it's value with the hiddenfield value from my _Layout.cshtml.

Is it possible to eliminate the use of the additional hiddenfield?

Thanks.

<script type="text/javascript">
    ($('#p1').val($('#playerChoice_1').val()));
</script>

    @using (Ajax.BeginForm(new AjaxOptions { OnSuccess = "LoadThankYou()" }))
        {
            @Html.ValidationSummary(true)
            <fieldset >

                <div style="FILTER:" class="fb-grouplabel fb-item fb-100-item-column">
                    @Html.LabelFor(model => model.Name)
                </div>
                <div class="fb-input-box editor-field">
                    @* <input id="item7_text_1" class="" name="text7" maxlength="254" placeholder="Enter your name"
                                       autocomplete="off" data-hint="" type="text" />*@
                    @Html.EditorFor(model => model.Name)
                    @Html.ValidationMessageFor(model => model.Name)
                </div>               

                <div style="FILTER:" class="fb-grouplabel fb-item fb-100-item-column">
                    @Html.LabelFor(model => model.Email)
                </div>
                <div class="fb-input-box editor-field">
                    @Html.EditorFor(model => model.Email)
                    @Html.ValidationMessageFor(model => model.Email)
                </div>

                <div style="FILTER:" class="fb-grouplabel fb-item fb-100-item-column">
                    @Html.LabelFor(model => model.Contact)
                </div>
                <div class="fb-input-box editor-field">
                    @Html.EditorFor(model => model.Contact)
                    @Html.ValidationMessageFor(model => model.Contact)
                </div>

                <div style="FILTER:" class="fb-grouplabel fb-item fb-100-item-column">
                    @Html.LabelFor(model => model.PersonalID)
                </div>
                <div class="fb-input-box editor-field">
                    @Html.EditorFor(model => model.PersonalID)
                    @Html.ValidationMessageFor(model => model.PersonalID)
                </div>

                <input type="hidden" id="p1" name="Choice_1" />

                <div style="MIN-HEIGHT: 1px" id="fb-submit-button-div" class="fb-item-alignment-left fb-footer">
                    @* <input style="BACKGROUND-IMAGE: url(theme/default/images/btn_submit.png)"
                            id="fb-submit-button" class="fb-button-special" type="submit" value="Submit" />*@
                    <input type="submit" value="Submit" />
                </div>
            </fieldset>
        }

Upvotes: 0

Views: 481

Answers (1)

Shyju
Shyju

Reputation: 218732

Do that on the ready event. So that it will execute after the DOM finished loading.

<script type="text/javascript">
  $(function(){
     $('#p1').val($('#playerChoice_1').val());
  });
</script>

If you want to avoid the hidden field, and you want this value in all the pages, you may consider storing the Value in Session variable one time and access it in your action methods wherever needed.

Upvotes: 2

Related Questions