【问题标题】:what is Difference between RedirectToAction() and View()RedirectToAction() 和 View() 有什么区别
【发布时间】:2020-05-16 06:22:44
【问题描述】:

我是asp.net MVC的初学者

我有一个这样的 Person 类

public partial class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Family { get; set; }
    public string WebSite { get; set; }
}

我有一个 ActionResult 创建如下所示

 public ActionResult Create(Person person)
    {
        db.Person.Add(person);
        db.SaveChanges();
        return RedirectToAction("Index");
        //retrun View (Index);
    }

当我使用 RedirectToAction 时,我的项目运行时没有任何错误,但是当我使用返回视图时,我面临错误。这些有什么区别??

注意:索引视图显示 Person 表中的所有信息

【问题讨论】:

  • 所以你不在乎 Index Action 里面有什么,想象一下你将 Index Action 里面的 View 更改为 Dashboard... 现在你需要到处改变...通过重定向对于一个动作,你没有与一个视图耦合......

标签: c# asp.net asp.net-mvc error-handling view


【解决方案1】:

在您的 Create 方法中,它应该是一个 POST 方法。使用重定向的更好的设计模式 后重定向获取模式。

请看这篇介绍Wikipedia : Post/Redirect/Get

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    相关资源
    最近更新 更多