简单MVC3问题 - 如何中检索从HTTPPOST,字典或表单值? [英] Simple MVC3 Question - How to retreive form values from HTTPPOST, dictionary or?

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

问题描述

简单的问题。我有了方法的MVC控制器:

Simple question. I have an mvc controller that has method:

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

的形式是一个非平凡形式用一个简单的文本框。

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

问题 - 如何在地球上我访问的参数值?我不是从View发布,后由外部来了。我假设有我有机会获得键/值对的集合。我试图Request.Params.Get(simpleTextBox);但它返回错误对不起,发生了错误处理您的请求。

Question - how on earth do I access the parameter values? 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. I tried Request.Params.Get("simpleTextBox"); but it returns error "Sorry, an error occurred while processing your request.".

我敢肯定有一个简单的答案。

I'm sure there's a simple answer to this.

推荐答案

您可以有你的控制器动作需要一个对象,它反映的形式输入名称,默认模式粘结剂将自动为您创建该对象:

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 ...
}

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

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