向ASP.NET MVC控制器中的操作方法发出HttpPost请求 [英] Make HttpPost request to an action method in an ASP.NET MVC controller

查看:750
本文介绍了向ASP.NET MVC控制器中的操作方法发出HttpPost请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一项功能,该功能需要在我们的应用程序中创建候选人的个人资料.有两个步骤/用户界面来创建候选人的个人资料:

I am trying to build a functionality where I need to a create a candidate's profile in our application. There are two steps/UI's to create a candidate's profile:

1-创建模板...供用户在其中输入候选人的信息.

1 - Create template...where the user enters candidate's information.

2-预览模板...,向用户显示其个人资料添加到我们的系统后的外观预览.

2 - Preview template...where the user will be shown a preview of how their profile would look like once they add the profile to our system.

我已经通过一个名为"CandidateController"的控制器创建了支持这些UI的视图,该控制器包含一些操作方法:

I have already created the views to support these UI's via a controller called "CandidateController" which contains few action methods:

1- [HttpGet]"Create"返回一个Create模板.

1- [HttpGet] "Create" that returns a Create template.

[HttpGet]
public ViewResult Create()

2- [HttpPost]预览",返回预览模板.

2- [HttpPost] "Preview" that returns a Preview template.

 [HttpPost]
 public ActionResult Preview(ProfileViewModel viewModel)

现在我需要实现的是在Create模板中具有一个按钮/链接,该按钮/链接将在控制器中调用操作方法[HttpPost] Preview.

Now what I need to implement is to have a button/link in the Create template that would call the action method [HttpPost] Preview in the controller.

挑战 我也想知道,如果能够从第一个创建模板调用HttpPost Preview操作方法,则模型绑定器是否可以为我加载ViewModel对象.

Challenge I am also wondering if there is a way that the model binder would load the ViewModel object for me if am able to call the HttpPost Preview action method from the first create template.

我正在寻找有关如何最好地实现这种功能的建议/帮助.

I am looking for a suggestion/help to how to best achieve this kind a functionality.

任何帮助将不胜感激.

推荐答案

挑战我也想知道模型绑定器是否有办法 如果能够调用HttpPost,则会为我加载ViewModel对象 从第一个创建模板预览操作方法.

Challenge I am also wondering if there is a way that the model binder would load the ViewModel object for me if am able to call the HttpPost Preview action method from the first create template.

您可以使用标准格式或AJAX调用来调用Preview POST操作,然后传递视图模型的所有属性值.您在此请求中传递的所有值将是将由默认模型绑定程序绑定的值. 一篇文章解释了默认模型绑定程序如何期望为更复杂的结构命名参数例如列表和字典.

You could use either a standard form or an AJAX call to invoke the Preview POST action and pass all the property values of the view model then. All the values you pass in this request will be the values that will be bound by the default model binder. Here's an article explaining how the default model binder expects the parameters to be named for more complex structure such as lists and dictionaries.

使用AJAX的示例:

$.ajax({
    url: '@Url.Action("Preview")',
    type: 'POST',
    data: { Prop1: 'value 1', Prop2: 'value 2' },
    success: function(result) {
        // TODO: do something with the result returned from the POST action
    }
});

如果您不想使用AJAX,则可以使用带有隐藏字段的标准表单:

If you don't want to use AJAX you could use a standard form with hidden fields:

@using (Html.BeginForm())
{
    @Html.Hidden("Prop1", "value 1")
    @Html.Hidden("Prop2", "value 2")
    ...
    <button type="submit">Preview</button>
}

这篇关于向ASP.NET MVC控制器中的操作方法发出HttpPost请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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