简体   繁体   中英

HttpPost and HttpGet

I need some help with my HttpPost. I dont know what to write at the HttpPost to use my working code in my view

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

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

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

all your html inputs should have "Name" attribute assigned to those.. eg

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

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.

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.

<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:

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) 
{

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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