【问题标题】:HttpPost and HttpGetHttpPost 和 HttpGet
【发布时间】:2023-03-15 13:33:02
【问题描述】:

我的 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() 
        {
           ??
        }

【问题讨论】:

    标签: c# http-post http-get


    【解决方案1】:

    你所有的 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>
    

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

    [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 操作中获得填充模型,但是由于你没有添加任何类型特定的视图,你可以通过 Form 集合访问发布的值。

    【讨论】:

      【解决方案2】:

      你在哪里?您需要输入:&lt;nameofclass&gt;.PasswordGenerator(...) '...' 应该是您的 PasswordGenerator() 方法所需的参数,&lt;nameofclass&gt; 应该是您的静态方法所在的类的名称。

      例如,您的代码可能看起来有点像这样,在您的视图中增加了一些连线:

          [HttpGet]
          public ActionResult Generate()
          {
              return View("Index");
          }
      
          [HttpPost]
          public ActionResult PasswordGenerator(PasswordModel model) 
          {
             <nameofclass>.PasswordGenerator(model.Password.Length, model.StrongPassword);
          }
      

      【讨论】:

        【解决方案3】:

        正如 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...) 
        {
        
        }
        

        如果参数的数量超出了您的承受能力,只需创建一个具有 [与您的表单字段匹配的属性的对象。即:

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

        并将其用作参数

        [HttpPost]
        public ActionResult PasswordGenerator(PasswordGeneratorArguments model) 
        {
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-10-28
          • 1970-01-01
          • 1970-01-01
          • 2021-07-29
          • 1970-01-01
          • 2013-05-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多