通常,您只需在两个操作之间共享视图。我猜您的操作看起来像这样(您提供的有关索引功能的信息越多,我的示例就越好):
public ActionResult Index()
{
return View();
}
[HttpPost, ActionName("Index")]
public ActionResult IndexPost()
{
if (!ModelState.IsValid)
{
ViewBag.ErrorMsg = "Your error message"; // i don't know what your error condition is, so I'm just using a typical example, where the model, which you didn't specify in your question, is valid.
}
return View("Index");
}
和 Index.cshtml
@if(!string.IsNullOrEmpty(ViewBag.ErrorMsg))
{
@ViewBag.ErrorMsg
}
@using(Html.BeginForm())
{
<!-- your form here. I'll just scaffold the editor since I don't know what your view model is -->
@Html.EditorForModel()
<button type="Submit">Submit</button>
}