Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MVC Newbie.
After an edit I want to save the changes and then update an image to be stored in a different class.

I have now debugged the inline code and realize I will need it in another controller. So how do I turn it into a method? Where do I put it (create a folder[mymethods]? There is probably some preferred place to keep these custom methods. In my VB days I would write this as a function or subroutine, now I see this as some type of action result. I've seen a list of several, would this be a fileresult()?

C#
       [HttpPost]
       [ValidateAntiForgeryToken]
       public ActionResult Edit(Task task)
       {
           if (ModelState.IsValid)
           {
               db.Entry(task).State = EntityState.Modified;
               db.SaveChanges();

// now create a new graph using the returned viewbag data
               var Mytopleft = 0;
               var Mytext = "";
               Bitmap mybmp = new Bitmap(10, 400);
               Graphics g = Graphics.FromImage(mybmp);
               // draw comma-delimited elements in multiple colors
               string[] chunks = Mytext.Split(',');
               var brush = new SolidBrush(Color.Black);
               SolidBrush[] brushes = new SolidBrush[]
                   {   new SolidBrush(Color.OldLace),
                       new SolidBrush(Color.LightSlateGray),
                       new SolidBrush(Color.LightBlue),
                       new SolidBrush(Color.LightGreen)
                   };
               int c = 0;
               foreach (var titem in ViewBag.Task)
               {
                   if (titem.SStart == null)
                   { c = 0; }
                   else if (titem.SStart != null && titem.SEnd == null)
                   { c = 1; }
                   else if (titem.SEnd != null && titem.SCert == null)
                   { c = 2; }
                   else if (titem.SCert != null)
                   { c = 3; }
   // create a new rectangle displaced by offset and with color
                   g.FillRectangle(brushes[c], Mytopleft, 0, 10, 10);
                   Mytopleft += 10;
               }
    //write to file   !!!!!!!!!!!!cannot write to root dir add \temp folder
                  mybmp.Save("C:\\temp\\lot1.bmp", System.Drawing.Imaging.ImageFormat.Gif);

               return RedirectToAction("Index");
           }
           ViewBag.BuilderID = new SelectList(db.Builders, "BuilderID", "BName", task.BuilderID);
           ViewBag.LotID = new SelectList(db.Lots, "LotID", "LotName", task.LotID);
           return View(task);
       }


Later I will write the bitmap into a db field.

So I create a method, something like Makegraph(Viewbag). Would I call it with a public FileResult makegraph(ViewBag)? Will I need to add a reference in the controller? Does it return(image)?


And most important where can I learn about this? MSDN doesn't seem to have meaningful examples.

Thank goodness for CODEPROJECT. and STACKOVERFLOW
Posted
Updated 27-Nov-13 1:53am
v3

1 solution

I tend to always have a Util class in my project, with static methods I am likely to use in more than one place. If this code is needed only for this model, put it on the model class. Overall I am at a loss as to why you're creating bitmaps on the fly anyhow ?

You can improve your logic like this:

C#
if (titem.SStart == null)
                   { c = 0; }
                   else if (titem.SEnd == null)
                   { c = 1; }
                   else if (titem.SCert == null)
                   { c = 2; }
                   else 
                   { c = 3; }



You're double checking things you already checked and know to be true by the time you get to them.
 
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