【问题标题】:using FormCollection使用 FormCollection
【发布时间】:2012-07-26 18:19:24
【问题描述】:

我目前正在向以下 .NET MVC 操作提交 json 请求

 [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult Signup(FormCollection collection)
    {

但是由于某种原因,该集合是空的。我知道数据正在发送,因为如果我将其更改为

[AcceptVerbs(HttpVerbs.Post)]
    public JsonResult Signup(String username, String email, String password)
    {

我正在使用表单集合,因为它允许我为该方法提供可选参数。 将来我可能没有用户名我可能有名字(例如)但我不能让旧的网址中断

请求以 NSMutableURLRequest 的形式来自 iOS 应用。 我是否遗漏了什么,或者我必须使用第二个选项并在添加新参数时创建不同的方法/在发布新应用程序更新时创建单独版本的 .NET MVC 应用程序。

是的,因为我正在发布这样的数据

NSMutableDictionary *sendData = [[NSMutableDictionary alloc] init];

        [sendData setValue:self.usernameTxt.text forKey:@"username"];
        [sendData setValue:self.emailTxt.text forKey:@"email"];
        [sendData setValue:self.deviceToken forKey:@"device_token"];

        //verify account details and retrieve the API Key
        responseData = [NSMutableData data];    

        NSString *stringUrl = kAppRequestUrl;

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[stringUrl stringByAppendingString:@"/SomePath"]]];
        [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPMethod:@"POST"];

        [request setHTTPBody: [sendData JSONData]];

【问题讨论】:

  • 您究竟是如何将数据发布到控制器的?是FormFieldName={JsonObject}的格式吗?

标签: objective-c asp.net-mvc


【解决方案1】:

FormCollection 表示您的Form 元素的集合。不要期望在其中接受 JSON 值。我会使用可选参数为您提供第二个选项(参数)。

[HttpPost]
public JsonResult Signup(String username="",string name="", String email, String password)
{
   //do stuff
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多