0

I am using asp.net core with razor engine on the front end and entity framework on the back end. The problem I am having is when I submit my form all the data I entered shows up as null in my method.

Here is my HTML Code where the user enters the info.

@model craigslist.Models.Auto
<form asp-controller="Auto" asp-action="AddAuto" method="post" role="form">
    <span asp-validation-for="Make"></span>
    <label asp-for="Make"></label>
    <input asp-for="Make"/>
    <span asp-validation-for="Model"></span>
    <label asp-for="Model"></label>
    <input asp-for="Model"/>
    <label asp-for="Part"></label>
    <input asp-for="Part"/>
    <label asp-for="Price"></label>
    <input asp-for="Price"/>
    <button type="submit">Add Auto or Part</button>
</form>

Here is the C# controller class method that is going to input the data in my db.

[HttpPost]
[Route("addAuto")]
public IActionResult AddAuto(Auto model) //model always shows up as null
{
    var user_id = HttpContext.Session.GetInt32("Id");
    if(ModelState.IsValid)
    {
        Auto auto = new Auto();
        auto.Make = model.Make;
        auto.Model = model.Model;
        auto.Part = model.Part;
        auto.Price = model.Price;
        auto.CreatedAt = DateTime.Now;
        auto.UpdatedAt = DateTime.Now;
        auto.UserId = (int)user_id;
        _context.Auto.Add(auto);
        _context.SaveChanges();
        return RedirectToAction("AutoInfo");
    }
    return View("Auto", model);
}

Here is my model for Auto

using System;
using System.ComponentModel.DataAnnotations;

namespace craigslist.Models
{
    public class Auto
    {
        public int Id { get; set; }

        [MinLength(3)]
        [Required(ErrorMessage ="Please enter your make")]
        public string Make { get; set; }

        [MinLength(3)]
        [Required(ErrorMessage ="Please enter your model")]
        public string Model { get; set; }
        public string Part { get; set; }

        [Required(ErrorMessage ="Please enter your price")]
        public int Price { get; set; }

        public int UserId {get; set;}
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
    }
}

Since Auto model in my controller always shows up as null nothing is ever added to my db. What am I doing wrong?

Aaron
  • 4,380
  • 19
  • 85
  • 141
  • @Usman asp-for is in every tag but in validation. The first one if for the label and the second one is for the input. If I take the label out than nothing will show up for the label. The input is there to get the data that is coming in as null currently – Aaron Feb 27 '17 at 17:58
  • You need to add [Bind(Include = "Property1,Property2,Property3...ect")] to your action method... – BillRuhl Feb 27 '17 at 18:03
  • @Roma, I just added the code above where I set my model in my view – Aaron Feb 27 '17 at 18:03
  • @BillRuhl, Can you explain a little bit more about Bind? – Aaron Feb 27 '17 at 18:05
  • I can see two points where you could make changes: 1. [StringLength] attribute instead of MinLength 2. [FromBody] instead proposed [Bind] - IActionResult AddAuto([FromBody]]Auto model) – Przemek Marcinkiewicz Feb 27 '17 at 18:10
  • @PrzemekMarcinkiewicz, what are the two? – Aaron Feb 27 '17 at 18:12
  • 1
    It have tried to reproduce your problem(without `BaseEntity`) but I got entered values. – Roman Feb 27 '17 at 18:12
  • @Aaron, I edited my answer because submited too early by mistake. – Przemek Marcinkiewicz Feb 27 '17 at 18:15
  • 1
    @BillRuhl: That is so categorically just wrong. First, no, you don't *need* to use `Bind` here, and second, it's a bad idea to use `Bind`, in general. http://cpratt.co/bind-is-evil/ – Chris Pratt Feb 27 '17 at 18:17
  • 1
    Could you attach BaseEntity class? – Przemek Marcinkiewicz Feb 27 '17 at 18:20
  • @PrzemekMarcinkiewicz: FWIW, `[FromBody]` is completely unnecessary as well. The action only allows post and the form is being posted. As a result, of course all of the data is coming from the request body. – Chris Pratt Feb 27 '17 at 18:25
  • @Aaron: There's nothing obviously wrong with your code. At the *very least*, `model` should not be null. Maybe not all the values will make to the properties, but it should at least be instantiated. I tend to agree that it's something to do with `BaseEntity`. We need to see the code for that. – Chris Pratt Feb 27 '17 at 18:27
  • I did not need BaseEntity. I removed it from the code – Aaron Feb 27 '17 at 18:46
  • In your HttpGet controller method, create and return an empty instance of your model object – Sparrow Feb 27 '17 at 18:50
  • @Aaron Another hint - make DateTime fields nullable, because there is no point where you assign values to them in your form OR initialize when instance of the model is created. – Przemek Marcinkiewicz Feb 28 '17 at 08:21

0 Answers0