将部分视图模型发布到同一MVC页面

时间:2014-08-09 13:11:35

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

从简单的MVC模板中,我修改了Manage.cshtml以包含部分_AccountSettingsPartial

<section id="accountSettings">            
    @Html.Partial("_AccountSettingsPartial")
</section>
@if (ViewBag.HasLocalPassword)
{
    @Html.Partial("_ChangePasswordPartial")
}

Manage.cshtml不会在任何地方使用@model,而是AccountController.cs中加载public ActionResult Manage(ManageMessageId? message)

_AccountSettingsPartial使用@model crapplication.Models.ManagePublicSettingsViewModel_ChangePasswordPartial使用@model crapplication.Models.ManageUserViewModel

最初,AccountController.cs只有ManageUserViewModel的帖子方法:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Manage(ManageUserViewModel model)

我试图为其他部分重载:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Manage(ManagePublicSettingsViewModel model)

但这不起作用,并在帖子上产生以下错误:

System.Reflection.AmbiguousMatchException: The current request for action 'Manage' on controller type 'AccountController' is ambiguous between the following action methods:
System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult] Manage(crapplication.Models.ManagePublicSettingsViewModel) on type crapplication.Controllers.AccountController
System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult] Manage(crapplication.Models.ManageUserViewModel) on type crapplication.Controllers.AccountController

如何让我的_AccountSettingsPartial页面从用户帐户管理门户发回数据?

1 个答案:

答案 0 :(得分:2)

您需要在部分中使用单独的表单,以便发布到不同的操作。 (或者你可以用JQuery等改变单个表单的动作,但这会更容易。)

@using (Html.BeginForm("ManagePublicSettings", "Account", FormMethod.Post))

...

@using (Html.BeginForm("ManageUser", "Account", FormMethod.Post))

然后,您需要对操作进行唯一命名。

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ManagePublicSettings(ManagePublicSettingsViewModel model)

...

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ManageUser(ManageUserViewModel model)