简体   繁体   English

如何在ASP.NET MVC 4 Razor中获取/处理POST数据?

[英]How to get/handle POST data in ASP.NET MVC 4 Razor?

I have a problem with getting/handling POST data from <form> in ASP.NET MVC 4 Razor project. 我在ASP.NET MVC 4 Razor项目中从<form>获取/处理POST数据时遇到问题。

What have I done? 我做了什么?

Don't argue at my code, I'm only testing ASP.NET MVC 4 features. 不要争论我的代码,我只测试ASP.NET MVC 4功能。

As you can see in the controller's code, I've done also a Model for user info: 如您在控制器代码中所见,我还为用户信息完成了一个Model

public class AuthForm
{
    public string Login { get; set; }
    public string Password { get; set; }
}

I thought, that ASP.NET if the model is correct will automatically parse data into the model, but mistaken. 我以为,如果模型正确,则ASP.NET会自动将数据解析到模型中,但会出错。

Then I tried to use .Request["name"] , but (inputs weren't empty): 然后,我尝试使用.Request["name"] ,但是(输入不为空):

在此处输入图片说明

Also I've tried to use such Attributes : 我也尝试过使用这样的属性

  • [HttpPost]
  • [AcceptVerbs(HttpVerbs.Post)]

But also no success! 但也没有成功!

What did I wrong? 我怎么了 Please, help me to fix my issues. 请帮我解决问题。

Thanks 谢谢

You need to use the helper methods so that MVC knows how to bind the values and then in the controller you will be able to use the model (as the model binder will figure it out for you) 您需要使用辅助方法,以便MVC知道如何绑定值,然后在控制器中您将能够使用模型(因为模型绑定器会为您解决)

eg 例如

@model Models.AuthForm
@{
    ViewBag.Title = "СЭЛФ";
}
@section home {

@using (Html.BeginForm("Auth", "Controller")) {
    <div class="control-group">
        @Html.LabelFor(model => model.Login, new { @class = "control-label" })
        <div class="controls">
            @Html.TextBoxFor(model => model.Login, new { @class = "input-large", autocapitalize = "off" }) 
            @Html.ValidationMessageFor(model => model.Login, "*", new { @class = "help-inline" })
       </div>
    </div>

    <div class="control-group">
        @Html.LabelFor(model => model.Password, new { @class = "control-label" })
        <div class="controls">
            @Html.PasswordFor(model => model.Password, new { @class= "input-large" }) 
            @Html.ValidationMessageFor(model => model.Password, "*", new { @class = "help-inline" })
        </div>
    </div>
    <div class="form-actions">
         <input type="submit" class="btn btn-primary" value="Log On" />
    </div>
}
}

Using the native HTML controls is an option but you'll need to do it in the same way as the helper methods above otherwise the model won't be populated. 使用本机HTML控件是一个选项,但您需要采用与上述帮助方法相同的方式进行操作,否则将不会填充模型。

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

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