简体   繁体   English

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

[英]Posting a Partial View Model to the Same MVC Page

From the bare-bones MVC template, I have modified Manage.cshtml to include a partial, the _AccountSettingsPartial : 从基本的MVC模板中,我修改了Manage.cshtml以包含部分_AccountSettingsPartial

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

Manage.cshtml does not use @model anywhere, and it is loaded in AccountController.cs , in public ActionResult Manage(ManageMessageId? message) . Manage.cshtml不会在任何地方使用@model ,而是将其加载到AccountController.cspublic ActionResult Manage(ManageMessageId? message)

_AccountSettingsPartial uses @model crapplication.Models.ManagePublicSettingsViewModel and _ChangePasswordPartial uses @model crapplication.Models.ManageUserViewModel . _AccountSettingsPartial使用@model crapplication.Models.ManagePublicSettingsViewModel_ChangePasswordPartial使用@model crapplication.Models.ManageUserViewModel

Originally, AccountController.cs had just a post method for ManageUserViewModel : 最初, AccountController.cs只是有一个ManageUserViewModel的post方法:

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

I have tried to overload it for the other partial: 我试图重载它的其他部分:

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

But this does not work, and produces the following error on a post: 但这不起作用,并在帖子上产生以下错误:

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

How can I make my _AccountSettingsPartial page post back data from the user account management portal? 如何使_AccountSettingsPartial页面从用户帐户管理门户回发数据?

You need to have separate forms in your partials, in order to post to different actions. 您需要在分部中使用单独的表单,以便发布到不同的动作。 (Or you could change the action of a single form with JQuery, etc., but this will be easier.) (或者您可以使用JQuery等更改单个表单的操作,但这会更容易。)

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

...

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

You then need to uniquely name the actions. 然后,您需要唯一地命名动作。

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

...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM