【问题标题】:multiple forms in an ASP.NET MVC view, and routing issuesASP.NET MVC 视图中的多个表单和路由问题
【发布时间】:2011-01-27 23:47:50
【问题描述】:

假设您有一个用于编辑三明治的 MVC 视图:三明治名称、价格等。此表单有自己的提交按钮。当您提交表单时,会调用 Edit POST 操作,更新三明治,并重新加载视图。

然后在同一个视图中,在“三明治编辑”表单下方,我们有一个配料下拉列表,旁边有一个“添加”按钮。如何将“添加成分”表单发布到不同的操作,然后重新加载“编辑”视图?

RedirectToAction("Edit") 在 URL 中放置了很多垃圾。

这是我尝试过的一种方法,但在 URL 中添加了垃圾:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult LoginRemoveAssociation(FormCollection values)
    {
        int webUserKey = Int32.Parse(values["WebUserKey"]);
        int associationKey = Int32.Parse(values["AssociationKey"]);
        db.DeleteWebUserAssociation(webUserKey, associationKey);
        return RedirectToAction("LoginEdit", new LoginEditViewModel(webUserKey, true));
    }

这是 URL 中 RedirectToAction 之后的垃圾:

https://localhost/mvc/Admin/Login/382?WebUser=Web.Data.Entities.WebUser&Associations=System.Data.Objects.ObjectQuery`1[Web.Data.Entities.Association]&WebUserAssociations=System.Data.Objects.DataClasses.EntityCollection`1[Web.Data.Entities.WebUserAssociation]&ManagementCompanies=System.Collections.Generic.List`1[Web.Data.Entities.ManagementCompany]&ManagementCompanyList=System.Web.Mvc.SelectList&AccessLevels=System.Collections.Generic.List`1[Web.Data.Entities.AccessLevel]&AccessLevelList=System.Web.Mvc.SelectList&PostMessage=Changes%20saved.

【问题讨论】:

  • "RedirectToAction("Edit") 在 URL 中放了很多垃圾".. 你能发一个“垃圾”的例子吗?

标签: asp.net-mvc


【解决方案1】:

您的网址中出现“垃圾”的原因是您将 LoginEditViewModel 传递给了编辑操作。 .Net 正在尝试将对象转换为名称,以便可以将其传递给参数。这就是为什么您会看到 Web.Data.Entities.......

您的编辑控制器是什么样的?

如果是这样的:

public ActionResult Edit(int id)

那么你的重定向动作应该是这样的:

return RedirectToAction("Edit", new { id = 1 });

而不是传递您的视图模型。

【讨论】:

  • 就是这样!谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
  • 2011-03-19
  • 1970-01-01
  • 2013-11-13
相关资源
最近更新 更多