【问题标题】:ASP.NET MVC Core - Post form to class parameterASP.NET MVC Core - 将表单发布到类参数
【发布时间】:2020-05-30 04:29:09
【问题描述】:
            <form method="post" id="modalForm" action="/Test/TestForm" target="myframe">
                <input type="text" name="id" required />
                <input type="text" name="name" required />
                <input type="submit" value="Post" onsubmit="alert('after post!');" />
            </form>

    [HttpPost]
    public IActionResult TestForm(Test test)
    {
        return View();
    }

    public class Test
    {
        public string id;
        public string name;
    }

需要在表单上进行哪些更改才能将值传递给 Test 类?目前这些值为空?我想保持简单,不使用 ASP.NET Core 助手。

【问题讨论】:

  • TestForm(Test test) 更改为TestForm([FromForm]Test test)
  • 你需要使用属性(get/set)而不是字段

标签: c# asp.net asp.net-mvc forms asp.net-core


【解决方案1】:

C# 提供了一种使用速记/自动属性的方法,您只需在属性内写入get;set;

    public class Test
    {
        public string id { get; set; }
        public string name { get; set; }
    }

【讨论】:

    猜你喜欢
    • 2018-12-28
    • 2014-04-09
    • 1970-01-01
    • 2017-01-18
    • 2023-04-09
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多