c# - Controller 返回当前 View

标签 c# asp.net-mvc asp.net-mvc-4

所以这很简单。我有一个调用 Controller 的 View ,该 Controller (取决于 bool 值)将返回一个新 View 。但如果没有,将保留当前 View 。

我该怎么做?

我当前的 Controller 代码是这样的:

public class MyController : Controller
{
    public ActionResult someView(bool myBool) // is really a string
    {
        if (myBool) // Is really checking if the string is empty
        {
            ModelState.AddModelError("message", "This is true");
        }
        else
        {
            return View();
        }
        return null;
    }
}

我知道我需要了解更多有关 mvc4 的知识,但请一起玩;-D

<小时/>

编辑献给我的天鹰队长^^

_Partial 页面代码:(感谢 John H)

@using (Html.BeginForm("someView", "My", FormMethod.Get))
{
    @Html.TextBox("text")
    <input type="submit" value='send' />
}

但我的问题的真正目标是找到一种方法返回调用它的View。希望没有模型你这将是正确的方式^^

最佳答案

返回 null 没有任何意义。这本质上是说“不返回任何 View ”。像这样的事情会起作用:

public class MyController : Controller
{
    public ActionResult SomeView(bool myBool)
    {
        if (myBool)
        {
            ModelState.AddModelError("message", "This is true");
            return View();
        }

        return RedirectToAction("SomeOtherView");
    }
}

这与我在其他问题中提到的 ModelState.IsValid 模式很接近(如下所示:

public ActionResult SomeView(SomeViewModel model)
{
    if (ModelState.IsValid)
    {
       // Model is valid so redirect to another action
       // to indicate success.
       return RedirectToAction("Success");
    }

    // This redisplays the form with any errors in the ModelState
    // collection that have been added by the model binder.
    return View(model);
}

关于c# - Controller 返回当前 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846603/

相关文章:

c# - 在 C# 中进行 SQL 解析?

c# - 在 for 循环中用匿名方法闭包

c# - 以编程方式调用 EntityDeploy 构建任务

asp.net-mvc - MVC T4 MvcTextTemplateHost和自定义的“ Controller ” T4模板

javascript - 通过ajax重定向到页面

c# - 为 EntityFramework6.Npgsql 指定数据库连接字符串 - Code First

c# - MVC 5 复选框返回 “False,false” 或 “false”

c# - 维护 MVC 中的 URL 发布到不同的操作

c# - MVC4 添加 header 到重定向响应不起作用

javascript - ASP.NET MVC Controller 结果返回查看