in my mvc 3 app with ef code first i have a database with 1 table
the column field is
ID
TIME1 Double TIME2 Double TIME3 Double
I need time3 is the result of the multiplication of time1 and time2 ... I did it with viewbag
Viewbag.Text = db.persons.Sum(o => o.Time1 * o.Time2);
But howunfortunately I can not associate the field Time3 the result ... here is my controller and view
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Person person)
{
person.Tempo3 = db.persons.Sum(o => o.Tempo1 * o.Tempo2);
if (ModelState.IsValid)
{
db.persons.Add(person);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
}
@using (Html.BeginForm()) {@Html.EditorFor(model => model.Tempo1) @Html.EditorFor(model => model.Tempo2) @Html.HiddenFor(model => model.Tempo3)
With HiddenFor the calculate don't work...how do this with linq query? thanks
Try something like this :
[HttpPost]
public ActionResult Create(Person person)
{
if (ModelState.IsValid)
{
newPerson = new db.persons {Tempo1 = person.Tempo1, Tempo2 = person.Tempo2};
newPerson.Tempo3 = peson.Tempo1 * person.Tempo2;
db.persons.Add(newPerson);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}