0

I want to use the same view to insert and update data in a database.

When I am in the "edit mode", I need to know what are the values before and after the edition (to know what has changed and perform operations based on that).

I cannot store the "before edit" data in the model, as there are not mapped and I cannot store that data in the controller as a new instance is created between get and post.

For instance I have no other idea that using a static class or hidden fields but it doesn't sound to me to be a good practice.

How to achieve that in a proper way?

Thanks

6
  • 1
    Have you look at Entity Framework, or any other framework? There are many tutorials to take you through this process. Do you want to store this data temporarily or do you want to persist this beyond the users session? Commented Oct 20, 2018 at 8:23
  • 3
    You are editing data so you should be using a view model so you can have whatever properties you want. In the post method you get the original data model from the db based on the ID and you can check what has changed (and if there is an issue with another user changing the data in the meantime, you could store a copy in Session) Commented Oct 20, 2018 at 8:23
  • Stephen that's what I also thought but I'm also wondering about storing the id. In the get part I'm providing the id to the method and after that? Store it in the view as an hidden field? Commented Oct 20, 2018 at 8:30
  • Yes, you need either a hidden input for the ID or you can add it as a route value in the form (otherwise when you submit, you would not know what record is being edited Commented Oct 20, 2018 at 8:45
  • 1
    @Manta hidden field is best way to do this. you just create a same method with different parameters in controller. like index() for get and index(Model model) for post. so when you submit your form ID will passed with model and you can edit that perticular record using single view.. Commented Oct 20, 2018 at 9:27

1 Answer 1

3

You can see the older data in
var data = context.DEMO_MAST.Where(x => x.ID == model.ID).FirstOrDefault(); and new data in Model and just using If Condition you can do AddEdit both things in single method.

AddEdit Method

 public int AddOrEdit(DEMO_MAST model)
            {
                try
                {
                    var data = context.DEMO_MAST.Where(x => x.ID == model.ID).FirstOrDefault();
                    if (data == null)
                    {
                        model.CREATED_ON = DateTime.Now;
                        this.context.DEMO_MAST.Add(model);
                    }
                    else
                    {
                        model.MODIFIED_ON = DateTime.Now;
                        this.context.Entry(data).CurrentValues.SetValues(model);
                    }
                    int flg = this.context.SaveChanges();
                    return flg;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }

Find

public DEMO_MAST Find(int ID)
{
  return context.DEMO_MAST.where(x=>x.ID==ID).FirstorDefault();
}

Controller

[HttpGet]
public ActionResult Index(int ID)
{
   DEMO_MAST model=new DEMO_MAST();
  if(ID>0)
  {
     model=Find(ID);   
  } 
return View(model);
}

[HttpPost]
public ActionResult Index(DEMO_MAST model)
{
  if(ModelState.IsValid)
  {
     int i = AddEdit(model); 
     if(i>0) 
     {
       ViewBag.Message="Record AddEdit Successfully";
     }
     else
     {
       ViewBag.Message="Error";
     }
  }
return View(model);
}

So when you run your application simple Index() view is display.

Index view for get data

   using (Html.BeginForm("Index", "Home"))
    {
        <div class="row">
            <div class="col-md-6 col-sm-3">
            </div>
            <div class="col-md-6 col-sm-9">
                <div id="sample_6_filter" class="dataTables_filter">
                    <label>
                        @Html.TextBox("ID", ViewBag.ID == null ? "" : (string)ViewBag.ID, new { @class = "form-control input-inline", placeholder = "ID" })
                        <button class="btn green-haze" type="submit" id="submit">
                            <i class="fa fa-search"></i>
                        </button>
                    </label>
                </div>
            </div>
        </div>
    }
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        @Html.HiddenFor(x => x.ID)
        @Html.HiddenFor(x => x.CREATED_ON)
        <div class="form-horizontal">
            
            <div class="form-group">
                <label class="control-label col-sm-4">Name</label>
                <div class="col-sm-5">
                    @Html.TextBoxFor(a => a.Name, new { @class = "form-control", @maxlength = "50" })
                </div>
                <div class="col-sm-3 text-danger">
                   
                </div>
            </div>
 <div class="form-group">
       <div class="col-sm-12 text-center">
                <button type="submit" name="command" value="createUser" class="btn btn-lg btn-green hvr-underline-from-center">Submit</button>
       </div>
  </div>
    }

You need to enter ID or any Uniq field of your database for get result in View. When you click on button the [HttpGet] Index method will be called and display records in view.

And after that you can modify your data.Click on submit so it will go for
'[HttPost] Index' remain things done automatically as per above code and methods.

Now you just need to set your data in view.. that's it.
Hope you understand!! Enjoy!!

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

1 Comment

Thanks for your help, yes I understood, no problem.

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.