HttpPost和HttpGet

时间:2013-03-11 11:52:53

标签: c# http-post http-get

我的HttpPost需要一些帮助。我不知道在HttpPost上写些什么来在我的视图中使用我的工作代码

我已经编写了一个代码来生成密码:

    private static string PasswordGenerator(int passwordLength, bool strongPassword)
{
    int seed = Random.Next(1, int.MaxValue);
    //const string allowedChars = "ABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
    const string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
    const string specialCharacters = @"!#$%&'()*+,-./:;<=>?@[\]_";

    var chars = new char[passwordLength];
    var rd = new Random(seed);

    for (var i = 0 ; i < passwordLength; i++)
    {
        // If we are to use special characters
        if (strongPassword && i % Random.Next(3, passwordLength) == 0 )
        {
            chars[i] = specialCharacters[rd.Next(0 , specialCharacters.Length)];
        }
        else
        {
            chars[i] = allowedChars[rd.Next(0 , allowedChars.Length)];
        }
    }

    return new string(chars);
}

现在我还有一个观点:

@{
    ViewBag.Title = "Index";
}

  @using (Html.BeginForm("Index", "PasswordGenerator", FormMethod.Post))
      {
<table class="passwordViewTable">


    <tr>
        <td>Password Length:</td>
        <td class="rechterKolom">  <input type="text" value="8" id="length_field"> (4 - 64 chars)</td>
    </tr>

    <tr>
        <td>Include Letters:</td>
        <td><input type="checkbox" id="checkbox_letters"  checked> ( e.g. abcdef) <br /></td>
    </tr>
    <tr>
        <td>Quantity:</td>
        <td>
            <select id="dropdown_quantity" class="styled">
               <option value="1">1</option>
               <option value="2">2</option>
            </select> <br />
       </td>
    </tr>

    <tr>
        <td><button type="submit" id="btn_submit">Submit</button> </td>
    </tr>
</table>
      }

现在我不知道在httpPost函数中写什么来让代码在视图上工作

  [HttpGet]
        public ActionResult Generate()
        {
            return View("Index");
        }

        [HttpPost]
        public ActionResult PasswordGenerator() 
        {
           ??
        }

3 个答案:

答案 0 :(得分:0)

所有html输入都应该为这些分配“Name”属性.. e.g。

 <tr>
        <td>Password Length:</td>
        <td class="rechterKolom">  
           <input type="text" name="length_field" value="8" id="length_field"> (4 - 64 chars)
        </td>
    </tr>

这是您必须在帖子中写的代码

[HttpPost]
  public ActionResult PasswordGenerator(FormCollection collection) 
 {
       //access your fields here like this 
       var length = collection["length_field"];
       // do the operation you need here
 }

基本上你应该在mvc中使用强类型视图,这样你就可以在你的POST动作中获得填充的模型,但是由于你没有添加任何类型特定的视图,你可以访问已发布的值来表示集合。

答案 1 :(得分:0)

你有哪些?您需要放置:<nameofclass>.PasswordGenerator(...)“...”应该是您的PasswordGenerator()方法所需的参数,<nameofclass>应该是静态方法所在类的名称。< / p>

例如,您的代码可能看起来有点像这样,您的视图中会有更多的连线:

    [HttpGet]
    public ActionResult Generate()
    {
        return View("Index");
    }

    [HttpPost]
    public ActionResult PasswordGenerator(PasswordModel model) 
    {
       <nameofclass>.PasswordGenerator(model.Password.Length, model.StrongPassword);
    }

答案 2 :(得分:0)

正如KD建议的那样,我会为所有表单元素添加名称属性。

<input type="text" name="length" value="8" id="length_field">
<input type="checkbox" id="includeLetters"  checked>

等...

但我会让模型绑定器通过指定参数类型为我们提供强类型值。

[HttpPost]
public ActionResult PasswordGenerator(int length, bool includeLetters, etc...) 
{

}

如果参数数量超出您认为合适的数量,只需使用[与您的表单字段匹配的属性创建一个对象。 IE:

public class PasswordGeneratorArguments {
    public int Length {get;set}
    public bool IncludeLetters {get;set}
    etc...
}

并将其用作参数

[HttpPost]
public ActionResult PasswordGenerator(PasswordGeneratorArguments model) 
{

}