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

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

问题描述

我有一个MVC控制器,它有这个Action方法:

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.

问题

我如何访问参数值?

我不是从View发帖,帖子来自外部。我假设有一组我可以访问的键/值对。

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天全站免登陆