[英]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的获取和发布请求均可用,则只需删除该属性,然后默认情况下,该属性将在获取和设置请求中可用。
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.