C#MVC返回查看(对象)的结果是查询字符串?

编程入门 行业动态 更新时间:2024-10-10 21:25:47
本文介绍了C#MVC返回查看(对象)的结果是查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这样的ActionResult方法:

I have this actionresult method:

public ActionResult MenuItemCreated(MenuItem item) { return View(item); }

这是我的看法:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MenuItem>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> MenuItemCreated </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>MenuItemCreated</h2> <%: Model.Caption %> is created succesfully </asp:Content>

我看到网页上是正确的(测试成功创建)。但我的查询字符串看起来像这样:

What I see on the page is correct (test is created successfully). But my querystring looks like this:

localhost:62602/Admin/MenuItemCreated/2?Caption=test&Link=%2Fclient

编辑:该的ActionResult从这个方法叫做:

The ActionResult is called from this method:

public ActionResult CreateMenuItem(FormCollection fc) { MenuItem menuItem = CreateMenuItemFrom(fc); SaveMenuItem(menuItem); return RedirectToAction("MenuItemCreated", menuItem); }

编辑II:

相应的视图:

<% using (Html.BeginForm("CreateMenuItem","Admin",FormMethod.Post)) {%> <%: Html.ValidationSummary(true) %> <fieldset> <legend>Fields</legend> <div class="editor-label"> <%: Html.LabelFor(model => model.Caption) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(model => model.Caption) %> <%: Html.ValidationMessageFor(model => model.Caption) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.Link) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(model => model.Link) %> <%: Html.ValidationMessageFor(model => model.Link) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.ParentId) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(model => model.ParentId) %> <%: Html.ValidationMessageFor(model => model.ParentId) %> </div> <p> <input type="submit" value="Create MenuItem" /> </p> </fieldset> <% } %>

这是为什么?我不希望要显示的查询字符串。

Why is this? I don't want the querystring to be shown.

推荐答案

问题是以下行:

return RedirectToAction("MenuItemCreated", menuItem);

当你执行重定向你不可错过这样复杂的对象。只有简单的标量的属性:

You cannot pass complex objects like this when you perform a redirect. Only simple scalar properties:

return RedirectToAction("MenuItemCreated", new { id = menuItem.Id });

然后重定向给出的ID来获取相应的菜单项的模型内的行动:

and then inside the action you are redirecting to fetch the corresponding menu item model given the id:

public ActionResult MenuItemCreated(int id) { var menuItem = _someRepository.GetMenuItem(id); ... }

更多推荐

C#MVC返回查看(对象)的结果是查询字符串?

本文发布于:2023-10-12 04:08:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   对象   结果是   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!