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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.