locked
SaveChanges to datebase when request has been approved RRS feed

  • Question

  • User-581354740 posted
    I'm building a holiday tracking web app.

    In my app employees can log on and request a holiday by creating holiday request form.

    When they create a request form, it must be approved by a manager. So I don't want changes saved to the database until Approved has been set to true!

    Do I do this by an if statement?

    Do I have to create another page where managers can view all requests and set a specific one to true?

    Here's my holiday request form model and Controller action. 

    As you can see I have tried inserted ''holidayRequestForm.Approved = true;" 

    but it is not working. 

          

    public partial class HolidayRequestForm
    {
        public int RequestID { get; set; }
        public int EmployeeID { get; set; }
        public System.DateTime StartDate { get; set; }
        public System.DateTime FinishDate { get; set; }
        public int HoursTaken { get; set; }
        public string Comments { get; set; }
        public int YearCreated { get; set; }
        public int MonthCreated { get; set; }
        public int DayCreated { get; set; }
        public Nullable<int> YearOfHoliday { get; set; }
        public Nullable<bool> Approved { get; set; }
    }






    public
    ActionResult Create() { return View(); } // POST: HolidayRequestForms/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "RequestID,EmployeeID,StartDate,FinishDate,HoursTaken,Comments,YearCreated,MonthCreated,DayCreated,YearOfHoliday,Approved")] HolidayRequestForm holidayRequestForm) { if (ModelState.IsValid) {
    holidayRequestForm.Approved = true; db.HolidayRequestForms.Add(holidayRequestForm); db.SaveChanges(); return RedirectToAction("Index"); } return View(holidayRequestForm); }
    Tuesday, January 22, 2019 9:15 AM

All replies

  • User753101303 posted

    Hi,

    What happens ? The form is shown again? It would point at ModelState.IsValid being false for some reason (with no message if not done properly ?)

    For this kind of workflow you usually save data with a "pending" statusa and then other people can see those requests and approve (or reject) them. It's not because they are not approved that they shouldn't be saved to the db. Managing those requests is part of the app data.

    Tuesday, January 22, 2019 10:04 AM
  • User-581354740 posted

    Hi,

    It just redirects to the list of holiday requests and changes have been made to the database. 

    How would I go about saving it as "pending"? Is there a tutorial? I'm very new to mvc so I'm finding this challenging. 

    The main issue is that these holiday request forms are connected to an employees data eg. Holiday hours taken. So I don't want those changes made until a manager could approve the request. 

    Tuesday, January 22, 2019 10:10 AM
  • User1120430333 posted

    How would I go about saving it as "pending"? Is there a tutorial? I'm very new to mvc so I'm finding this challenging.
    The main issue is that these holiday request forms are connected to an employees data eg. Holiday hours taken. So I don't want those changes made until a manager could approve the request.

    There is no tutorial on this and MVC, a UI design pattern,  has nothing to do with setting status on a table record, which is business logic . Your  database table design should have a Status column on it,  and the Status is set to Pending when a record is added to the table.

    I suggest that you take MVC, a UI design pattern,  out of the picture for now and figure out the  when, where and why you are going to set the record's Status

    MVC is just a UI design pattern there is nothing magical about it if you understand the pattern the purpose.

    https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models-views-and-controllers-cs

    <copied>

    An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic. 

    A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). Everything else should be contained in the model.

    In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.

    <end>

    Tuesday, January 22, 2019 9:23 PM
  • User-581354740 posted
    I actually figured it out earlier!! I just used a sql query. In the view I used to calculate the hours taken per employee. I just added a WHERE clause So it only adds to Hours Taken when a manager sets it to approved
    Tuesday, January 22, 2019 9:28 PM
  • User1120430333 posted

    I actually figured it out earlier!! I just used a sql query. In the view I used to calculate the hours taken per employee. I just added a WHERE clause So it only adds to Hours Taken when a manager sets it to approved

    But to me, you still don't understand how to use the MVC UI design pattern principles  effectively, if you have data access being done directly by the controller, which is tight coupling  as opposed to loose coupling and also not understanding SoC in regards to MVC. 

    Wednesday, January 23, 2019 12:08 AM
  • User-581354740 posted
    I’m still extremely new to mvc, less than a months experience so I’m still learning the basics so I’ll definitely take your notes on board about MVC UI design pattern principles, coupling and SoC. I am quite eager to learn it, thank you!!
    Wednesday, January 23, 2019 12:13 AM
  • User1520731567 posted

    Hi Conor8630,

    What is your error message?

    Could you please post more detail?

    Best Regards.

    Yuki Tao

    Wednesday, January 23, 2019 10:20 AM