C# 是否可以重定向到另一个传递它的操作以及我们当前的模型HttpPost?
因此,我正在试验ASP.NET MVC,我有以下代码:C# 是否可以重定向到另一个传递它的操作以及我们当前的模型HttpPost?,c#,.net,asp.net,asp.net-mvc,asp.net-mvc-3,C#,.net,Asp.net,Asp.net Mvc,Asp.net Mvc 3,因此,我正在试验ASP.NET MVC,我有以下代码: public class TrollController : Controller { public ActionResult Index() { var trollModel = new TrollModel() { Name = "Default Trol
public class TrollController : Controller
{
public ActionResult Index()
{
var trollModel = new TrollModel()
{
Name = "Default Troll",
Age = "666"
};
return View(trollModel);
}
[HttpPost]
public ActionResult Index(TrollModel trollModel)
{
return View(trollModel);
}
public ActionResult CreateNew()
{
return View();
}
[HttpPost]
public ActionResult CreateNew(TrollModel trollModel)
{
return RedirectToAction("Index");
}
}
这个想法是要有一个索引页面,显示我们巨魔的年龄以及他的名字
有一个操作允许我们创建一个巨魔,在创建它之后,我们应该返回到索引页面,但这次是使用我们的数据,而不是默认的数据
有没有办法将
TrollModel
CreateNew(TrollModel-TrollModel)
接收到的索引(TrollModel-TrollModel)
传递给用户?如果是,如何保存?在CreateNew中,必须存在某种持久性,例如,巨魔可能保存在数据库中。它还必须具有某种ID。因此索引方法可以更改为
public ActionResult Index(string id)
{
TrollModel trollModel;
if (string.IsNullOrEmpty(id))
{
trollModel = new TrollModel()
{
Name = "Default Troll",
Age = "666"
};
}
else
{
trollModel = GetFromPersisted(id);
}
return View(trollModel);
}
在新的世界里
[HttpPost]
public ActionResult CreateNew(TrollModel trollModel)
{
return RedirectToAction("Index", new {id = "theNewId"});
}
在CreateNew中,必须有某种持久性,例如,巨魔可能保存在数据库中。它还必须具有某种ID。因此索引方法可以更改为
public ActionResult Index(string id)
{
TrollModel trollModel;
if (string.IsNullOrEmpty(id))
{
trollModel = new TrollModel()
{
Name = "Default Troll",
Age = "666"
};
}
else
{
trollModel = GetFromPersisted(id);
}
return View(trollModel);
}
在新的世界里
[HttpPost]
public ActionResult CreateNew(TrollModel trollModel)
{
return RedirectToAction("Index", new {id = "theNewId"});
}
最好的方法是将troll持久化到服务器上的某个位置(数据库?),然后在重定向时只将id传递给索引操作,以便它可以取回它。另一种可能是使用TempData或Session:
[HttpPost]
public ActionResult CreateNew(TrollModel trollModel)
{
TempData["troll"] = trollModel;
return RedirectToAction("Index");
}
public ActionResult Index()
{
var trollModel = TempData["troll"] as TrollModel;
if (trollModel == null)
{
trollModel = new TrollModel
{
Name = "Default Troll",
Age = "666"
};
}
return View(trollModel);
}
TempData将仅在一次重定向中存活,并在后续请求中自动退出,而会话将在会话的所有HTTP请求中持久化
还有一种可能性是在重定向时将troll对象的所有属性作为查询字符串参数传递:
[HttpPost]
public ActionResult CreateNew(TrollModel trollModel)
{
return RedirectToAction("Index", new
{
Age = trollModel.Age,
Name = trollModel.Name
});
}
public ActionResult Index(TrollModel trollModel)
{
if (trollModel == null)
{
trollModel = new TrollModel
{
Name = "Default Troll",
Age = "666"
};
}
return View(trollModel);
}
现在,您可能需要重命名Index POST操作,因为您不能有两个具有相同名称和参数的方法:
[HttpPost]
[ActionName("Index")]
public ActionResult HandleATroll(TrollModel trollModel)
{
return View(trollModel);
}
最好的方法是将troll持久化到服务器上的某个位置(数据库?),然后在重定向时只将id传递给索引操作,以便它可以取回它。另一种可能是使用TempData或Session:
[HttpPost]
public ActionResult CreateNew(TrollModel trollModel)
{
TempData["troll"] = trollModel;
return RedirectToAction("Index");
}
public ActionResult Index()
{
var trollModel = TempData["troll"] as TrollModel;
if (trollModel == null)
{
trollModel = new TrollModel
{
Name = "Default Troll",
Age = "666"
};
}
return View(trollModel);
}
TempData将仅在一次重定向中存活,并在后续请求中自动退出,而会话将在会话的所有HTTP请求中持久化
还有一种可能性是在重定向时将troll对象的所有属性作为查询字符串参数传递:
[HttpPost]
public ActionResult CreateNew(TrollModel trollModel)
{
return RedirectToAction("Index", new
{
Age = trollModel.Age,
Name = trollModel.Name
});
}
public ActionResult Index(TrollModel trollModel)
{
if (trollModel == null)
{
trollModel = new TrollModel
{
Name = "Default Troll",
Age = "666"
};
}
return View(trollModel);
}
现在,您可能需要重命名Index POST操作,因为您不能有两个具有相同名称和参数的方法:
[HttpPost]
[ActionName("Index")]
public ActionResult HandleATroll(TrollModel trollModel)
{
return View(trollModel);
}
我不想持久化任何东西。好吧,让我们假设对象使用某种方法(TempData)生存下来,如果不持久化,在当前请求之后会发生什么。如果对象没有持久化,“CreateNew”会做什么?@amit\u g我同意。动作名称似乎有些混乱;有一个CreateNew并没有真正创建任何东西,还有一个Index的行为并不像Index操作,而是更像一个Details操作(即它的模型是TrollModel,而不是IEnumerable或TrollModel[])是的,你可以说所选的名称很糟糕。我不想保留任何东西。好的,假设对象使用某种方法(TempData)生存下来,如果当前请求没有持久化,那么在当前请求之后会发生什么。如果对象没有持久化,“CreateNew”会做什么?@amit\u g我同意。动作名称似乎有些混乱;有一个CreateNew并不真正创建任何内容,还有一个索引的行为与索引操作不太相似,但更像是一个细节操作(即,它的模型是TrollModel,而不是IEnumerable或TrollModel[])是的,您可以说所选的名称非常糟糕。