简体   繁体   English

HttpPost和HttpGet

[英]HttpPost and HttpGet

I need some help with my HttpPost. 我的HttpPost需要一些帮助。 I dont know what to write at the HttpPost to use my working code in my view 我不知道在HttpPost上写什么才能在我的视图中使用我的工作代码

I have write a code to generate a password: 我已经写了一个代码来生成密码:

    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);
}

Now i have also a view: 现在我也有一个看法:

@{
    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>
      }

Now i don't know what to write at the httpPost function to let the code work on the view 现在我不知道在httpPost函数中写什么,以使代码在视图上工作

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

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

all your html inputs should have "Name" attribute assigned to those.. eg 您所有的html输入都应为这些输入分配“名称”属性。例如

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

here is the code you have to write in post 这是您必须在帖子中编写的代码

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

Basically you should use strongly type views in mvc so you can get the filled model in your POST action but as you haven't added any type specific view you can access the posted values thorugh Form collection. 基本上,您应该在mvc中使用强类型视图,以便可以在POST操作中获取填充的模型,但是由于尚未添加任何特定于类型的视图,因此可以通过Form集合访问发布的值。

Where you have the ?? 你在哪里?? you need to put: <nameofclass>.PasswordGenerator(...) the '...' should be the parameters your PasswordGenerator() method requires, and the <nameofclass> should be the name of the class where your static method resides. 您需要输入: <nameofclass>.PasswordGenerator(...) '...'应该是PasswordGenerator()方法所需的参数,而<nameofclass>应该是静态方法所在类的名称。

For example your code could look a little like this, with a bit more wire up in your view: 例如,您的代码可能看起来像这样,在您的视图中还有一些连接:

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

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

As KD suggested, I would add name attributes to all the form elements. 正如KD建议的那样,我将名称属性添加到所有表单元素中。

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

etc... 等等...

but I would let the model binder give us strongly typed values by specifying the argument type. 但是我会让模型绑定器通过指定参数类型为我们提供强类型的值。

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

}

If the number of arguments exceeds what you feel comfortable with, simply create an object with [properties that match your form field. 如果参数数量超出您的允许范围,则只需使用[与表单字段匹配的属性创建一个对象。 IE: IE浏览器:

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

and use that as the argument 并以此作为参数

[HttpPost]
public ActionResult PasswordGenerator(PasswordGeneratorArguments model) 
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM