Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
code:-

[HttpPost]
       [AllowAnonymous]
       [ValidateAntiForgeryToken]
       public ActionResult Index(HTMLDisplayViewModel model)
       {
           try
           {

                   // Verification
                   if (ModelState.IsValid)
                   {


                   this.databaseManager.Insert_HTML_Content(model.HtmlContent);
                       //context.Entities.Add(entity);

                   // save info.

                       // Commit database.
                       this.databaseManager.SaveChanges();

                       // Settings.
                       model.HTMLContentList = this.databaseManager.display_all_HTML_Content().ToList();
                       model.Message = "Information successfully!! saved.";
                       model.IsValid = true;

                   }
                   else
                   {
                       if (string.IsNullOrEmpty(model.HtmlContent))
                       {
                           // Settings.
                           model.HTMLContentList = new List<display_all_HTML_Content_Result>();
                           model.Message = "Require field can not be emptied";
                           model.IsValid = false;
                       }
                   }
           }
           catch (Exception ex)
           {
               // Info
               Console.Write(ex);
           }

           // Info
           return this.View(model);
       }


What I have tried:

i want to add and update text through same textarea in MVC 5
Posted
Updated 6-Sep-20 0:54am

1 solution

You can have following logic in place:
For entity that you save, keep an ID as one of the parameter (mapped to unique field in DB). Don't make this field as mandatory for model validation.

Now, whenever it's a new entity for insert, this ID would be null and defined once inserted in DB. If an entity is being updated, this ID will have some data mapping to existing data in DB.

You can use this ID field value as a flag to know and implement if it should be insert or update flow. Keep this ID field as hidden field in your view:
C#
@Html.HiddenFor(m => m.Id)

Your controller code would change to:
C#
if (ModelState.IsValid)
{
    if (!model.Id.HasValue){ 
    {
       // Do insert ...
    } else {
       // Do update ...
    }
}


Try out.

BTW, you should keep insert & update separate and have single responsibilities for each flow from controller method point of view. It's cleaner. Examples:
Tutorial: Implement CRUD Functionality with the Entity Framework in ASP.NET MVC | Microsoft Docs[^]
Perform List, Insert, Update and Delete in a Single View in ASP.NET MVC | BinaryIntellect Knowledge Base[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900