c# - 将查询字符串传递给 mvc View 操作

标签 c# asp.net-mvc model-view-controller razor query-string

当模型在后期操作中无效时,我想返回到带有错误消息以及查询字符串的同一页面

我的代码在这里

public ActionResult Action1(string Key)
{

    // do something
}

[HttpPost]
public ActionResult Action1(Model user)
{
    if (ModelState.IsValid)
                {
    // do some stuff here
    }
    else
     // redirect to same page with query string key and also error message
}

请通过显示错误消息建议我在模型无效时需要添加的行以留在同一页面中。

最佳答案

public ActionResult Action1(Model user)
{
    if (ModelState.IsValid)
    {
       // all is okay
    }
    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return View(model);
}

关于c# - 将查询字符串传递给 mvc View 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33476511/

相关文章:

ASP.NET MVC 是否已准备好用于业务应用程序(集成第 3 方控件/组件)?

c# - 从不同的文件夹渲染部分(不共享)

javascript - 尝试通过 signalR 发送消息时,客户端为空

asp.net-mvc - ASP.Net MVC 强类型部分 View 和继承属性

c# - 通过 PIA 接口(interface)类型获取 CLSID

c# - 菜鸟 WPF 数据绑定(bind) - 为什么我的 DataGrid 不自动生成列?

javascript - 带有 javascript 问题的 Razor

c# - 了解 Action 和UriQuery

c# - 在 C# 中保持大小写不变的情况下替换文本

javascript - 按条件显示 kendo UI TreeView 的上下文菜单