我有这个actionresult方法:
public ActionResult MenuItemCreated(MenuItem item)
{
return View(item);
}
Run Code Online (Sandbox Code Playgroud)
这是我的观点:
<%@ 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>
Run Code Online (Sandbox Code Playgroud)
我在页面上看到的内容是正确的(测试创建成功).但我的查询符如下所示:
http://localhost:62602/Admin/MenuItemCreated/2?Caption=test&Link=%2Fclient
Run Code Online (Sandbox Code Playgroud)
编辑:从这个方法调用ActionResult:
public ActionResult CreateMenuItem(FormCollection fc)
{
MenuItem menuItem = CreateMenuItemFrom(fc);
SaveMenuItem(menuItem);
return RedirectToAction("MenuItemCreated", menuItem);
}
Run Code Online (Sandbox Code Playgroud)
编辑二:
对应观点:
<% 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>
<% } %>
Run Code Online (Sandbox Code Playgroud)
为什么是这样?我不希望显示查询字符串.
问题是以下几行:
return RedirectToAction("MenuItemCreated", menuItem);
Run Code Online (Sandbox Code Playgroud)
执行重定向时,无法传递此类复杂对象.只有简单的标量属性:
return RedirectToAction("MenuItemCreated", new {
id = menuItem.Id
});
Run Code Online (Sandbox Code Playgroud)
然后在动作内部重定向以获取给定id的相应菜单项模型:
public ActionResult MenuItemCreated(int id)
{
var menuItem = _someRepository.GetMenuItem(id);
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10595 次 |
| 最近记录: |