MVC概念帮助 - 将内联代码更改为方法 [英] MVC concept help - change inline code to method
问题描述
MVC新手。
编辑后我想保存更改,然后更新图像以存储在不同的类中。
我现在调试了内联代码并意识到我需要在另一个控制器中使用它。那我怎么把它变成一个方法呢?我在哪里放(创建一个文件夹[mymethods]?可能有一些首选的地方来保存这些自定义方法。在我的VB时代,我会把它写成一个函数或子程序,现在我把它看作某种类型的动作结果。我看过几个列表,这是一个fileresult()吗?
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()?
[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);
}
稍后我会将位图写入数据库字段。
所以我创建了一个类似Makegraph(Viewbag)的方法。我会用公共FileResult makegraph(ViewBag)调用它吗?我需要在控制器中添加引用吗?是否返回(图片)?
最重要的是我在哪里可以了解这一点? MSDN似乎没有有意义的例子。
感谢CODEPROJECT。和STACKOVERFLOW
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
推荐答案
我倾向于在我的项目中总是有一个Util类,我可能会在多个地方使用静态方法。如果仅对此模型需要此代码,请将其放在模型类上。总的来说,我不知道为什么你在飞行中创建位图无论如何?
你可以改善这样的逻辑:
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:
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.
这篇关于MVC概念帮助 - 将内联代码更改为方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!