简体   繁体   English

回发后如何保持单选按钮的填充

[英]How to keep radio buttons populated after postback

I have a page that has dynamic rows that show and hide based on radio button selection.我有一个页面,其中包含基于单选按钮选择显示和隐藏的动态行。 There is also validation making sure that each field (that is shown) is populated and has a valid value.还有验证确保每个字段(显示)都已填充并具有有效值。 When the user submits the page it validates the fields and posts back and while the text box values are stored all the radio buttons become deselected and the dynamic rows are all hidden.当用户提交页面时,它会验证字段并回发,当文本框值被存储时,所有单选按钮都被取消选择,动态行全部被隐藏。 I was wondering how to maintain the selection of the radio buttons and the visible/hidden fields after a 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>

this is one of my radio buttons on the cshtml page这是我在 cshtml 页面上的单选按钮之一

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

This is the jquery that shows and hides fields based on the radio button selection这是基于单选按钮选择显示和隐藏字段的 jquery

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

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

else
{

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

and this is the validation after pressing the submit button at the end of the form.这是在表单末尾按下提交按钮后的验证。

Any help would be appreciated.任何帮助,将不胜感激。

On a high level, what you need to do is the following:在高层次上,您需要做的是以下事情:

  1. Treat your CREATE view the same you would do an UPDATE view.像对待 UPDATE 视图一样对待 CREATE 视图。 You should add Razor syntax for any radiobuttons that should be clicked.您应该为应该单击的任何单选按钮添加 Razor 语法。
  2. You should use either Razor or Javascript to figure out which dynamic contents to show or hide based on the selected radio buttons.您应该使用 Razor 或 Javascript 根据选定的单选按钮确定要显示或隐藏的动态内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM