在我的_Layout.cshtml共享视图中,我有一个hiddenfield,我想保留一个值供多个页面使用。现在,在页面之一中,我必须提交一个需要该特定hiddenfield值的表单。我以为我只需要从_Layout.cshtml中传递hiddenfield值,但是不确定如何分配它,下面的方法可以工作,但是我正在创建一个附加的hiddenfield,并使用来自我的_Layout.cshtml。

是否可以消除对其他隐藏字段的使用?

谢谢。

<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>
        }

最佳答案

ready事件上执行此操作。这样它将在DOM完成加载后执行。

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


如果要避免隐藏字段,并且希望在所有页面中使用此值,则可以考虑一次性将“值存储在Session”变量中,并在需要时在操作方法中访问它。

10-08 16:18