向 ASP.NET MVC 控制器中的操作方法发出 HttpPost 请求
我正在尝试构建一个功能,我需要在我们的应用程序中创建候选人的个人资料。创建候选人的个人资料有两个步骤/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用标准表单或 AJAX 调用来调用 Preview POST 操作,然后传递视图模型的所有属性值。您在此请求中传递的所有值都将是由默认模型绑定器绑定的值。这是一篇文章,解释默认模型绑定器如何为更复杂的结构命名参数例如列表和字典。
AJAX 示例:
如果您不想使用 AJAX,您可以使用带有隐藏字段的标准表单:
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:
If you don't want to use AJAX you could use a standard form with hidden fields:
好的,这里是我必须解决的选项:
如果您不想使用 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:
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...
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.