微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 控制器后期操作不接收模型

非常基本的型号:

public class Person
{
    public string Name;
    public int Age;
}

而且非常简单的观点:

@model DynWebPOC.Models.Person

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Hello,@Model.Name
<br/>
You're getting old at @Model.Age years old Now!

@using(Html.BeginForm("Index","Test",FormMethod.Post))
{
    <fieldset>       
        <label for="name" style="color: whitesmoke">Name:</label>    
        @Html.TextBoxFor(m => m.Name)
        <br/>
        <label for="age" style="color: whitesmoke">Age:</label>

        @Html.TextBoxFor(m => m.Age)

        <br/>
        <input type="submit" value="Submit"/>
    </fieldset>
}

一个非常简单的控制器:

public class TestController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
        object model = new Person {Name = "foo",Age = 44};
        return View(model);
    }


   [HttpPost]
   public ActionResult Index(Person person)
   {
       return View();
   }
}

加载屏幕时,值正确绑定到页面.但是当我按下提交按钮时,person对象具有age和amp;的所有空值.名称.

因为我使用了Html.TextBoxFor,它不应该正确设置所有绑定并且对象应该自动绑定回POST吗?它在GET中绑定得很好..

我在Html.BeginForm()的调用中错过了什么?

解决方法

您必须在模型中创建属性

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

代替

public class Person
{
    public string Name;
    public int Age;
}

ASP.net MVC仅绑定属性.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐