0

I am new in MVC. I am doing a program that edits existing records in a mongodb. My program is not updating the record.

This is the edit method:

[HttpPost]
public async Task<ActionResult> Edit(BioCardModel model)
{

    await _bioCardServices.UpdateBioCard( model.cardId ,model);

    return RedirectToAction("Index");
} 

This is the method that updates the record in the mongo db

public async Task UpdateBioCard(Guid value, [FromBody]BioCardModel card)
{
    var db = ConnectToMongo();
    var collection = db.GetCollection<BioCardModel>(_collection);

    var filter = Builders<BioCardModel>.Filter.Eq("cardId", value);
    var update = Builders<BioCardModel>.Update
        .Set(b => b.name, card.name)
        .Set(b => b.firstName, card.firstName)
        .Set(b => b.lastName, card.lastName)
        .Set(b => b.title, card.title)
        .Set(b => b.lifeSpan, card.lifeSpan)
        .Set(b => b.bio, card.bio)
        .Set(b => b.bio, card.anecdote)
        .Set(b => b.imageFront, card.imageFront)
        .Set(b => b.imageBack, card.imageBack);
    await collection.UpdateOneAsync(filter, update);
}

My view is a simple form that is populated with a record data.

ekad
  • 14,436
  • 26
  • 44
  • 46
felipe
  • 3
  • 4

1 Answers1

0

It looks like the view code is missing from this post, but I'm going to guess that your view doesn't contain all the necessary fields for your model. Make sure that your rendering all the fields you want included in your post into the view.

You can use hidden fields to output your primary key into the form to ensure it gets posted back to the server. Here's a good link to an explanation on the difference between the hidden fields in ASP.NET MVC

What is the difference between Html.Hidden and Html.HiddenFor

Community
  • 1
  • 1