在我的 html 页面中,页面顶部有一个默认警告,提示填充所有输入区域。
<span style="color:gray" class="help-block">Please enter value for all input fields.</span>
这是一个向数据库添加一些内容的页面。当我单击“添加”按钮时,它会在我的 C#
代码中重定向 ActionResult
。在其中,根据输入的输入值,我的代码要么将内容插入数据库,要么重新加载添加页面,这意味着插入失败。
[HttpPost]
public ActionResult InsertStuff(Stuff stuff)
{
if (stuffManager.InsertStuff(stuff))
return RedirectToAction("ListStuffs", "StuffController"); //successfully inserted!!
else
return RedirectToAction("AddNewStuffForm", "StuffController"); //reload page, here is where i'd like to change warning in html
}
在其他部分,我想加载相同的 AddNewStuffForm
页面,但我希望警告将颜色从灰色更改为红色,如下所示:
<span style="color:red" class="help-block">Please enter value for all input fields.</span>
我不知道该怎么做,正如我推断的那样,我需要将一些内容发送到我的 C#
的 else
部分中的 html
代码,但我不知道该怎么做。我确实做了一些研究,一些解决方案提供了使用诸如 Page_Load
之类的方法。然而,我确实是 html
的初学者,并且我在提供的解决方案方面遇到了困难。任何人都可以简单地解释一下该怎么做吗?提前致谢。
最佳答案
有很多方法可以做到这一点,您可以将检查值存储在 ViewData 中以用于其他部分,并且可以重新检查渲染 View 上的 ViewData 值并相应地更改您的样式,如下所示
<span style="color:@(ViewData["fail"])%;" class="help-block">Please enter value for all input fields.</span>
谢谢:)
关于javascript - 重新加载时使用 C# 更改 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39976425/