C#MVC返回查看(对象)的结果是查询字符串? [英] C# MVC Return view(object) results in a querystring?

查看:119
本文介绍了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:

http://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返回查看(对象)的结果是查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆