保存动态视图ASP.NET

时间:2018-02-28 20:45:42

标签: javascript c# asp.net-core-mvc

我是ASP.NET Core的新手,我正在尝试根据下拉选项制作一个包含动态文本框的页面。下拉选项会更改用户输入内容的输入类型。代码:

<form>
<script type="text/javascript">
    function ShowHideDiv() {
        var searchSelection = document.getElementById("searchSelection");
        var rewardsBox = document.getElementById("userId");
        var emailBox = document.getElementById("email");
        var phoneBox = document.getElementById("phone");

        rewardsBox.style.display = searchSelection.value == "userId" ? "block" : "none";
        emailBox.style.display = searchSelection.value == "email" ? "block" : "none";
        phoneBox.style.display = searchSelection.value == "phone" ? "block" : "none";
    }
</script>

<select class="form-control" id="searchSelection" onchange="ShowHideDiv()">
    <option value="userId">ID</option>
    <option value="email">Email</option>
    <option value="phone">Phone</option>
</select>

<div class="form-group">
    <label for="value">Value:</label>
        <div id="userId" style="display:block">
            <input asp-for="UserId" type="text" class="form-control clearable" id="idText" Placeholder="0123456789">
        </div>
        <div id="email" style="display:none">
            <input asp-for="Email" type="text" class="form-control clearable" id="emailText" Placeholder="janedoe@hallmark.com">
        </div>
        <div id="phone" style="display:none">
            <input asp-for="Phone" type="text" class="form-control clearable" id="phoneText" Placeholder="8751239876">
        </div>
</div>
</form>

提交表单时,代码返回一个包含检索到的用户的视图,表单保留在页面上。表单保持填写,这是我想要的,但下拉列表将自身重新设置为ID作为选定的值并显示ID的文本框(由于它是一个新的视图,这是预期的)。有没有办法保存视图,以便选择然后使用javascript更改的内容将出现在新视图中?

0 个答案:

没有答案