返回带有模型和查询字符串的视图

发布于 2025-01-08 12:24:06 字数 275 浏览 2 评论 0原文

我试图从同时具有查询字符串和模型的控制器返回视图

return View("BillingReport.aspx/?report=" + fc["report_selector"], bilDet);

,但这给了我一个未找到页面的运行时错误,因为它在网址末尾附加了 .aspx 等。

RedirectToAction() 没有执行此操作的选项。
有没有办法做到这一点,或者 mvc3 是否限制我们使用查询字符串或模型

I am trying to return to view from a controller that has both querystring and model

return View("BillingReport.aspx/?report=" + fc["report_selector"], bilDet);

but this gives me a runtime error of page not found as it appends .aspx etc at the end of the url.

RedirectToAction() doesnt have an option to do it.
Is there a way to do it or does mvc3 limit us to using either a query string or a model

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反话 2025-01-15 12:24:06

MVC 不支持您正在寻找的内容,

但我不明白您为什么要重定向到带有 ModelValues 的 URL。

任何重定向都是 GET 请求,因此您可以构建模型并从该操作返回视图。

View() 需要一个视图名称和与其关联的模型。

Redirect() 或 RedirectToAction() 用于将 url 重定向到另一个控制器/操作。因此,您无法传递模型。即使您尝试传递模型,它也会将模型属性附加为查询字符串参数。

MVC does not support what you are looking for,

But I dont understand why do you want to Redirect To a URL with ModelValues.

Any redirection is a GET request, so you can construct the model and return View from that action.

View() expects a view name and model associated with it.

Redirect() or RedirectToAction() are used to redirect the url to another controller/action. So you can not pass a model.Even if you will try to pass model it will append model properties as querystring parameters.

猥琐帝 2025-01-15 12:24:06

以下是您想要使用模型和查询字符串的原因:查询字符串允许您为用户提供保存带有状态信息的 URL 的方法。该模型允许您传递大量非扁平数据。因此,我认为如何在 MVC 5 中执行此操作(可能不适用于旧版本,但可能有效):

对视图使用 2 个操作而不是 1 个操作。使用第一个通过 RedirectToAction 设置查询字符串。使用第二个操作将模型返回到视图。然后,您可以通过会话状态将模型从第一个操作传递到第二个操作。这是示例代码:

public ActionResult Index(string email){
    Session["stuff"]=Load(email);
    return RedirectToAction("View1action", new { email = email, color = "red" });
}

public ActionResult View1action(string email){
    return View("View1",(StuffClass)Session["stuff"]);
}

Here is a reason why you would want use the model and the querystring: the querystring allows you to give user way to save URL with state information. The model allows you to pass a lot of non-flattened data around. So here is I think how to do this in MVC 5 (maybe does not work for older versions, but probably does):

Use 2 actions rather than 1 for the view. use first one to set querystring via RedirectToAction. Use second action to return model to the view. And you pass the model from the first action to the second action via session state. Here is example code:

public ActionResult Index(string email){
    Session["stuff"]=Load(email);
    return RedirectToAction("View1action", new { email = email, color = "red" });
}

public ActionResult View1action(string email){
    return View("View1",(StuffClass)Session["stuff"]);
}
话少心凉 2025-01-15 12:24:06

我同意玛纳斯的回答,如果我是你,我会考虑改变设计(如果可能的话)。
作为旁注,可以使用以下技术:

TempData["bilDet"] = bilDet;
return RedirectToAction(....);   // your controller, action etc.

在该操作中,您可以检索 TempData。 TempData 将自动删除。

但还要查看: ASP.NET MVC - TempData - 好或坏的做法

I agree with Manas's answer and if I were you I would consider changing the design if possible.
As a side note, the following technique is possible:

TempData["bilDet"] = bilDet;
return RedirectToAction(....);   // your controller, action etc.

On the action you can then retrieve your TempData. TempData will automatically be removed.

But also check out: ASP.NET MVC - TempData - Good or bad practice

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文