簡體   English   中英

回發后如何保持單選按鈕的填充

[英]How to keep radio buttons populated after postback

我有一個頁面,其中包含基於單選按鈕選擇顯示和隱藏的動態行。 還有驗證確保每個字段(顯示)都已填充並具有有效值。 當用戶提交頁面時,它會驗證字段並回發,當文本框值被存儲時,所有單選按鈕都被取消選擇,動態行全部被隱藏。 我想知道如何在回發后保持對單選按鈕和可見/隱藏字段的選擇。

<div class="form-group">
    <label asp-for="HaveYouEnteredTheProperty">Have you entered the property?</label>
    <div asp-for="HaveYouEnteredTheProperty" class="btn-group btn-group-toggle" data-toggle="buttons" style="width:100%">
        <label style="width:50%" class="btn btn-success">
            <input type="radio" name="VisitDetailsRadio" id="propYes" value="Yes"/> Yes 
        </label>
        <label style="width:50%" class="btn btn-danger">
            <input type="radio" name="VisitDetailsRadio" id="propNo" value="No" /> No 
        </label>
    </div>
    <span asp-validation-for="HaveYouEnteredTheProperty" class="text-danger"></span>
</div>

這是我在 cshtml 頁面上的單選按鈕之一

 $("input[name=VisitDetailsRadio]").change(function () {
    if ($(this).val() == "No") {
        $(".VisitDetailsExpandedYes").hide();
        $(".VisitDetailsExpandedNo").show();
        $(".EnteredProperty").hide();
    }
    else {
        $(".VisitDetailsExpandedYes").show();
        $(".VisitDetailsExpandedNo").hide();
        $(".EnteredProperty").show();
         }
});

這是基於單選按鈕選擇顯示和隱藏字段的 jquery

if (results.IsValid)
{
    _context.Results.Add(model);
    _context.SaveChanges();

    ModelState.Clear();
    return View();
}

else
{

    results.AddToModelState(ModelState, null);
    return View(model);
}

這是在表單末尾按下提交按鈕后的驗證。

任何幫助,將不勝感激。

在高層次上,您需要做的是以下事情:

  1. 像對待 UPDATE 視圖一樣對待 CREATE 視圖。 您應該為應該單擊的任何單選按鈕添加 Razor 語法。
  2. 您應該使用 Razor 或 Javascript 根據選定的單選按鈕確定要顯示或隱藏的動態內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM