简体   繁体   中英

Model always NULL when POST from View to Controller

I have this code in my controller

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

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

            return View("List");
        }

Here's the model

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;
}

And here's the View

<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 will not autobind the model and always return NULL, I checked the FormCollection and it seems to have all the correct values, what am I doing wrong?

Properties in the model should be defined as auto property.

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;}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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