如何从 HTTPPOST、字典或检索表单值? [英] How to retrieve form values from HTTPPOST, dictionary or?

查看:28
本文介绍了如何从 HTTPPOST、字典或检索表单值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有此操作方法的 MVC 控制器:

I have an MVC controller that has this Action Method:

[HttpPost]
public ActionResult SubmitAction()
{
     // Get Post Params Here
 ... return something ...
}

该表单是一个带有简单文本框的非平凡表单.

The form is a non-trivial form with a simple textbox.

问题

我如何访问参数值?

我不是从视图发帖,帖子是从外部发出的.我假设有一组我可以访问的键/值对.

I am not posting from a View, the post is coming externally. I'm assuming there is a collection of key/value pairs I have access to.

我尝试了 Request.Params.Get("simpleTextBox"); 但它返回错误抱歉,处理您的请求时出错.".

I tried Request.Params.Get("simpleTextBox"); but it returns error "Sorry, an error occurred while processing your request.".

推荐答案

您可以让您的控制器操作采用一个对象,该对象将反映表单输入名称,并且默认模型绑定器将自动为您创建此对象:

You could have your controller action take an object which would reflect the form input names and the default model binder will automatically create this object for you:

[HttpPost]
public ActionResult SubmitAction(SomeModel model)
{
    var value1 = model.SimpleProp1;
    var value2 = model.SimpleProp2;
    var value3 = model.ComplexProp1.SimpleProp1;
    ...

    ... return something ...
}

另一种(显然更丑)的方法是:

Another (obviously uglier) way is:

[HttpPost]
public ActionResult SubmitAction()
{
    var value1 = Request["SimpleProp1"];
    var value2 = Request["SimpleProp2"];
    var value3 = Request["ComplexProp1.SimpleProp1"];
    ...

    ... return something ...
}

这篇关于如何从 HTTPPOST、字典或检索表单值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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