“The Id field is required” validation message on Create; Id not set to [Required]

前端 未结 16 1322
盖世英雄少女心
盖世英雄少女心 2020-12-07 17:18

This is happening when I try to create the entity using a Create style action in Asp.Net MVC 2.

The POCO has the following properties:

public int Id          


        
相关标签:
16条回答
  • 2020-12-07 18:08

    I ran into this issue with a form in which I was adding "objects" to a list dynamically. Therefore, our users were able to add, remove or update. It all worked well except for the cases when new items were created. For this reason, in my case, excluding the Id property was not an option. The solution was to make the ID Nullable:

    public int? Id { get; set; }
    

    This way existing items will have a value and new ones will have null instead. Good stuff.

    0 讨论(0)
  • 2020-12-07 18:12

    Great question and answers, saved my ... behind. I need to add something, though:

    Instead of

    [Bind(Exclude = "Id")]
    

    I think it's better to use

    [Bind(Include = "Prop1, Prop2, Prop3, etc")]
    

    .. where Prop1, Prop2 and Prop3 are THE ONLY properties that you want to be bound at the action level.

    Since this is white-listing as opposed to black-listing. White-listing is better, safer. This way you also solve the risk of over posting and under posting too. See Brad Wilson's post.

    0 讨论(0)
  • 2020-12-07 18:12

    The Id should be sent from the client as 0. The model does not have a problem with Id=0, this is the default of int. The problem is that he does not see the value coming from the client or it's coming with space or null. I have an input hidden that represents the Id (or in complex object NestedPropertyId/NestedProperty.Id) so I make sure it starts with a value of zero.

    <input type="hidden" id="id" value="0" />
    <input type="hidden" id="eventId" value="0"/>
    <input type="hidden" id="contactId" value="0"/>
    

    Also when Ireset the form to Add New entity in the client side, I make sure to initialize the hidden with zero.

    Hope this helps somone.

    Efy

    0 讨论(0)
  • 2020-12-07 18:14

    I had the same issue and managed to solve it by simply passing an empty instance of the view model to the view during the GET call of my Create method.

    //GET: DocumentTypes/{Controller}/Create
        public ActionResult Create()
        {
            return View(new DocumentTypeViewModel());
        }
    
        //POST: DocumentTypes/{Controller}/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(DocumentTypeViewModel viewModel)
        {
            if(ModelState.IsValid) {
    
                var result = AddDocumentType(viewModel);
                if(!result.HasValidationErrors) {
                    return RedirectToAction("Index");
                }
    
                ModelState.Update(result);
    
            }
    
            return View(viewModel);
        }
    

    And then in my view, I ensure that I have

    @Html.HiddenFor(m => m.ID)
    

    This way I don't have to explicitly specify Bind attributes

    0 讨论(0)
提交回复
热议问题