首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RedirectToAction未按预期工作

RedirectToAction未按预期工作
EN

Stack Overflow用户
提问于 2011-11-21 19:14:21
回答 1查看 2.3K关注 0票数 1

我有一个简单的MVC3应用程序,我想从服务中检索一些配置详细信息,允许用户编辑和保存配置。

如果在保存过程中检测到任何错误,这些错误将被返回并报告给用户。

问题是无法调用包含错误的配置,并且当前保存的值正在重新显示。

单步执行代码,当检测到错误时,它应该使用传递的config对象重定向到自身,但它没有这样做,并使用不带参数的方法。

有人能看出我哪里错了吗?

下面是要调用的两个控制器方法:

代码语言:javascript
复制
//
// 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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-21 19:17:34

代码语言:javascript
复制
return RedirectToAction("Index", config);

重定向时不能传递像这样的复杂对象。您需要逐个传递查询字符串参数:

代码语言:javascript
复制
return RedirectToAction("Index", new {
    Prop1 = config.Prop1,
    Prop2 = config.Prop2,
    ...
});

另外,我在你的控制器中也看不到Index操作。也许是打字错误。我注意到的另一件事是,您有一个Edit GET操作,您可能正在尝试重定向到该操作,但此Edit操作不接受任何参数,因此它看起来很奇怪。如果你正在尝试重定向到POST Edit操作,那么这显然是不可能的,因为重定向本质上总是在GET上。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8210865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档