【问题标题】:Model always NULL when POST from View to Controller从视图到控制器 POST 时模型始终为 NULL
【发布时间】:2019-04-27 06:21:31
【问题描述】:

我的控制器中有这段代码

        public IActionResult Create()
        {
            return View(new Partner());
        }

        [HttpPost]
        //[ValidateAntiForgeryToken]
        public IActionResult Create(Partner model, IFormCollection pForm)
        {
            _uow.Partner.Insert(model);

            return View("List");
        }

这是模型

public class Partner
{
    public Guid ID;
    public string Type;
    public string Code;
    public string Name;
    public string IDCard;
    public DateTime BirthDate;
    public bool CardStatus;
    public string Address;
    public string Email;
}

这是视图

<form role="form" asp-action="Create">
                    <div class="box-body">
                        <div class="form-group">
                            <label>Type</label>
                            <input asp-for="Type" type="text" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>Code</label>
                            <input asp-for="Code" type="text" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>Name</label>
                            <input asp-for="Name" type="text" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>ID Card</label>
                            <input asp-for="IDCard" type="text" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>Email</label>
                            <div class="input-group">
                                <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                                <input asp-for="Email" type="email" class="form-control">
                            </div>
                        </div>
                        <div class="checkbox">
                            <label>
                                <input asp-for="CardStatus" type="checkbox"> Card Status
                            </label>
                        </div>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                        <a href="/Partner/List/"><button type="button" class="btn btn-default">Back</button></a>&nbsp
                        <button type="submit" class="btn btn-primary">Submit</button>
                    </div>
                </form>

HTTPOST 不会自动绑定模型并且总是返回 NULL,我检查了 FormCollection 并且它似乎具有所有正确的值,我做错了什么?

【问题讨论】:

  • 在Create([Frombody]Partner model, IFormCollection pForm)上使用这个。

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


【解决方案1】:

模型中的属性应定义为自动属性。

public class Partner
{
    public Guid ID {get; set;}
    public string Type {get; set;}
    public string Code {get; set;}
    public string Name {get; set;}
    public string IDCard {get; set;}
    public DateTime BirthDate {get; set;}
    public bool CardStatus {get; set;}
    public string Address {get; set;}
    public string Email {get; set;}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 2016-02-10
    • 2017-08-30
    相关资源
    最近更新 更多