我有一个简单的MVC3应用程序,我想从服务中检索一些配置详细信息,允许用户编辑和保存配置。
如果在保存过程中检测到任何错误,这些错误将被返回并报告给用户。
问题是无法调用包含错误的配置,并且当前保存的值正在重新显示。
单步执行代码,当检测到错误时,它应该使用传递的config对象重定向到自身,但它没有这样做,并使用不带参数的方法。
有人能看出我哪里错了吗?
下面是要调用的两个控制器方法:
//
// GET: /Settings/Edit/
public ActionResult Edit()
{
SettingsViewModel config = null;
// Set up a channel factory to use the webHTTPBinding
using (WebChannelFactory<IChangeService> serviceChannel =
new WebChannelFactory<IChangeService>(new Uri(baseServiceUrl)))
{
// Retrieve the current configuration from the service for editing
IChangeService channel = serviceChannel.CreateChannel();
config = channel.GetSysConfig();
}
ViewBag.Message = "Service Configuration";
return View(config);
}
//
// POST: /Settings/Edit/
[HttpPost]
public ActionResult Edit( SettingsViewModel config)
{
try
{
if (ModelState.IsValid)
{
// Set up a channel factory to use the webHTTPBinding
using (WebChannelFactory<IChangeService> serviceChannel = new WebChannelFactory<IChangeService>(new Uri(baseServiceUrl)))
{
IChangeService channel = serviceChannel.CreateChannel();
config = channel.SetSysConfig(config);
// Check for any errors returned by the service
if (config.ConfigErrors != null && config.ConfigErrors.Count > 0)
{
// Force the redisplay of the page displaying the errors at the top
return RedirectToAction("Edit", config);
}
}
}
return RedirectToAction("Index", config);
}
catch
{
return View();
}
}发布于 2011-11-21 19:17:34
return RedirectToAction("Index", config);重定向时不能传递像这样的复杂对象。您需要逐个传递查询字符串参数:
return RedirectToAction("Index", new {
Prop1 = config.Prop1,
Prop2 = config.Prop2,
...
});另外,我在你的控制器中也看不到Index操作。也许是打字错误。我注意到的另一件事是,您有一个Edit GET操作,您可能正在尝试重定向到该操作,但此Edit操作不接受任何参数,因此它看起来很奇怪。如果你正在尝试重定向到POST Edit操作,那么这显然是不可能的,因为重定向本质上总是在GET上。
https://stackoverflow.com/questions/8210865
复制相似问题