向 ASP.NET MVC 控制器中的操作方法发出 HttpPost 请求

发布于 2024-12-20 08:41:47 字数 676 浏览 2 评论 0原文

我正在尝试构建一个功能,我需要在我们的应用程序中创建候选人的个人资料。创建候选人的个人资料有两个步骤/UI:

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

2 - 预览模板...用户将个人资料添加到我们的系统后,将在其中预览其个人资料的外观。

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

1- [HttpGet]“Create”,返回一个 Create 模板。

[HttpGet]
public ViewResult Create()

2- [HttpPost]“预览”返回预览模板。

 [HttpPost]
 public ActionResult Preview(ProfileViewModel viewModel)

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

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

我正在寻找有关如何最好地实现此类功能的建议/帮助。

任何帮助将不胜感激。

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 - Create template...where the user enters candidate's information.

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.

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

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

[HttpGet]
public ViewResult Create()

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

 [HttpPost]
 public ActionResult Preview(ProfileViewModel viewModel)

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.

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.

Any help will be deeply appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若沐 2024-12-27 08:41:47

挑战我也想知道是否有一种方法可以让模型绑定器
如果我能够调用 HttpPost ,将为我加载 ViewModel 对象
预览第一个创建模板的操作方法。

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

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,您可以使用带有隐藏字段的标准表单:

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

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.

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.

Example with 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
    }
});

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>
}
貪欢 2024-12-27 08:41:47

好的,这里是我必须解决的选项:

  • 正如 Darin 建议的那样,您可以使用 $.ajax(options) 来采用不引人注目的方式,但是问题是,只有当您想做一个时,您可能才想采用这种方式部分页面更新或者如果您想在同一视图中更新/转储新的 html。
  • 如果您不想使用 Ajax,而不是使用隐藏字段,您可以简单地使用 MVC 中的 TempData 属性,这就是我使用 TempData 实现目标功能的方法。 ps如下...

    <前><代码>[HttpPost]
    公共 ActionResult 创建(ViewModel viewModel)
    {
    this.TempData["个人资料"] = viewModel;
    返回 RedirectToAction("预览");
    }

    公共 ActionResult 预览()
    {

    if (TempData["profile"] != null)
    {
    返回视图((ViewModel)TempData [“配置文件”]);
    }

    // 处理无效请求...
    返回空值;
    }

所以,这个解决方案对我来说效果很好,我没有编写任何 JavaScript 或不必要的 HTML。感谢达林引导我找到一个起点。

OK so here are the options that I had to get around:

  • As Darin suggested you may go with the unobtrusive way by using $.ajax(options), however the thing is you might want to go this way only if you want to do a partial page update or if you want to work on updating/dumping new html in the same view.
  • And if you don't want to use the Ajax, instead of using Hidden fields, you can simply use the TempData property in MVC, this is how I implemented my targeted functionality using TempData. p.s.below...

    [HttpPost]       
    public ActionResult Create(ViewModel viewModel)
    {
        this.TempData["profile"] = viewModel;
        return RedirectToAction("Preview");
    }
    
    
    public ActionResult Preview()
    {            
    
        if (TempData["profile"] != null)
        {
            return View((ViewModel)TempData["profile"]);
        }
    
        // Handle invalid request...
        return null;
    }
    

So, this solution worked pretty well for me, where I did not write any JavaScript or unnecessary HTML. AND thanks Darin for directing me to a starting point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文