c# - 回发后如何保持单选按钮的填充
问题描述
我有一个页面,其中包含基于单选按钮选择显示和隐藏的动态行。还有验证确保每个字段(显示)都已填充并具有有效值。当用户提交页面时,它会验证字段并回发,并且在存储文本框值的同时,所有单选按钮都将被取消选中,并且动态行都被隐藏。我想知道如何在回发后保持对单选按钮和可见/隐藏字段的选择。
<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);
}
这是在表单末尾按下提交按钮后的验证。
任何帮助,将不胜感激。
解决方案
在高层次上,您需要做的是以下事情:
- 像对待 UPDATE 视图一样对待 CREATE 视图。您应该为应该单击的任何单选按钮添加 Razor 语法。
- 您应该使用 Razor 或 Javascript 来根据选定的单选按钮确定要显示或隐藏哪些动态内容。
推荐阅读
- angular - 单元测试错误:跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https
- django - Django:条纹和 POST 请求
- mongoose - Mongoose JS 日期字段始终填充当前日期
- android - 用于在 Spring MVC 中下载多个图像的 Web 服务
- c++ - 如何检查 int 中是否设置了一个位?
- python - PyQT Graph -- 图形显示巨大
- c# - 如何从 MVC 5 中的 FileResult 和锚标记下载文件?
- python-3.x - 如何在熊猫中将一周的第一天设置为星期二
- typescript - 打字稿和传单 - 地图缩放超出范围
- sql-server - SQL Server 导入和导出向导 - 更改并发设置