简体   繁体   English

如何在ASP.NET C#/ Razor中传递表单后变量?

[英]How do you pass post form variables in ASP.NET C#/Razor?

I'm new to C#/Razor and don't know how to pass form data using the post method. 我是C#/ Razor的新手,并且不知道如何使用post方法传递表单数据。 Here's what I've tried: 这是我尝试过的:

In login.cshtml: 在login.cshtml中:

string username = Request.Form["username"];
string password = Request.Form["password"];

And

string username = Request.Form.Get("username");
string password = Request.Form.Get("password");

In _AppStart.cshtml, I tried: 在_AppStart.cshtml中,我尝试过:

AppState["username"] = HttpContext.Current.Request.Form["username"];
AppState["password"] = HttpContext.Current.Request.Form["password"];

All return nothing. 一切都不返回。

Here's the form elements in login.cshtml: 这是login.cshtml中的表单元素:

<form action="index.cshtml" method="post" data-ajax="false">
    <input type="text" name="username" placeholder="Username">
    <input type="password" name="password" placeholder="Password">
    <button type="submit">Login</button>
</form>

If you need to pass data between MVC controller and View then you have to let Asp know which action an controller to call. 如果需要在MVC控制器和View之间传递数据,则必须让Asp知道控制器要调用哪个动作。

To accomplish this you could use something like BeginForms in razor an specify the needed information. 为此,您可以在razor中使用诸如BeginForms之类的东西,指定所需的信息。

This would look like this: 看起来像这样:

    @Html.BeginForm("YourAction", "YourController", FormMethod.Post){ 
      @Html.TextBoxFor(employee => employee.FirstName);
      @Html.TextBoxFor(employee => employee.LastName);
      @Html.TextBoxFor(employee => employee.Age);
      <input type="submit" value="Edit" />
    }

Following this snippet you can see that you need A Controller and you name an ActionResult according to the name given here furthermore you can specify if you want to have the action only when posting or only for get forms 在此代码段之后,您会看到您需要一个Controller并根据此处提供的名称命名一个ActionResult,此外,您可以指定是仅在发布时还是仅在获取表单时才执行该操作

An possibile exemple could be the edit like the following code 一个可能的例子可能是如下代码所示的编辑

    [HttpPost]
    public ActionResult YourAction(Employee emp)
    {
        if (ModelState.IsValid)
        {
            // do smth.
        }

        return View("Name Of the view");
    }

Note you need to define this in your Controller and that the Attribute HttpPost lets Asp know that this is a post only ActionResult. 请注意,您需要在Controller中进行定义,并且HttpPost属性使Asp知道这是仅发布的ActionResult。 This means that only post requests can use this Method. 这意味着只有发布请求可以使用此方法。 Furthermore 此外

if you wish to have both get and post requests available for this ActionResult then you can simply delete the Attribute then per default it will be available in get and set requests. 如果您希望此ActionResult的获取和发布请求均可用,则只需删除该属性,然后默认情况下,该属性将在获取和设置请求中可用。

You could look at this forum entry 您可以查看此论坛条目

in Input Type "submit add formaction="ActionMethodName" 在输入类型“提交添加formaction =“ ActionMethodName”

 @using (Html.BeginForm("MethodName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
   {
        <input type="file" name="postedFile"/>
        <input type="submit"  **formaction="UploadData"** value="UploadData"/>

   }

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

相关问题 如何使用 ASP.NET 内核制作 Razor 页面和 C# 代码框架? - How to do you make both a Razor Page and C# code framework with ASP.NET Core? 如何使用提交按钮发布 SQL 数据 asp.net c# razor - How do I post SQL data using submit button in asp.net c# razor 如何使用 ASP.NET Core / Razor 通过单击按钮运行在 Razor 页面上编写的 C#? - How do you run C# written on a Razor page from a button click with ASP.NET Core / Razor? ASP.Net Razor C#变量可访问所有页面 - ASP.Net Razor C# Variables Accessible to All Pages 如何使用AjaxFileUpload.js将HTML输入文件传递到C#ASP.Net? - How do you pass HTML input files to C# ASP.Net using AjaxFileUpload.js? 如何在ASP.NET MVC 3剃刀视图中访问应用程序变量? - How do you access application variables in asp.net mvc 3 razor views? 如何在ASP.NET C#中使用if(IsPostBack) - How do you use if (IsPostBack) in ASP.NET C# 如何将输入文本参数传递给ASP.NET Razor中的C#函数? - How to pass input text parameter to C# function in ASP.NET Razor? Razor,asp.net,C#:如何验证所需的表单字段并允许HTML代码段? - Razor, asp.net, C#: how to validate a form field that is required and allows HTML snippets? c# asp.net 如何为只读剃须刀制作下拉列表 - how to make dropdownlistfor readonly razor c# asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM