How to make simple calculations using model elements and form input in ASP.net MVC 3?

I'm new to programming and ASP.net MVC 3, so don't be surprised at my lack of knowledge about this. Ok, I want to multiply two decimal places. One decimal place comes from the form that the user fills out, and another decimal comes from the Model class (gets it from the database).

I have two model classes called RATE and PROJECTMATERIAL. The RATE class has an element called Amount , which indicates the size of the bet, and the PROJECTMATERIAL class has a value. The classes are related, and I want to say variable1 = quantity * Rates.amount and return variable1 to my pointers, Delete, Details. I don't want to store variable1 in my database, but I just want to display in my views ..... but I don't know how and where to do it.

Code from the project material class ..

public class ProjectMaterial
{
    public int ProjectMaterialID { get; set; }

    [Required]
    [Display(Name = "Scope Name")]
    public int? ScopeID { get; set; }

    [Required]
    [Display(Name = "Rate Code")]
    public int? RateID { get; set; }

    [Required]
    [Display(Name = "Quantity")]
    public decimal Quantity { get; set; }


    public virtual Scope Scopes { get; set; }
    public virtual Rate Rates { get; set; }

}

Code from scope class.

public class Rate
{
    public int RateID { get; set; }


    [Required]
    [Display(Name = "Rate Code")]
    public int RateCode { get; set; }

    [Required]
    [Display(Name = "Unit")]
    public string Unit { get; set; }

    [Required]
    [Display(Name = "Description")]
    public string Description { get; set; }

    [Required]
    [Display(Name = "Amount")]
    public decimal Amount { get; set; }

    public virtual ICollection<ProjectMaterial> ProjectMaterials { get; set; }
}

Code from the project controller class ...

public class ProjectMaterialController : Controller
{
    private ContructorContext db = new ContructorContext();

    //
    // GET: /ProjectMaterial/

    public ViewResult Index()
    {
        var projectmaterials = db.ProjectMaterials.Include(p => p.Scopes).Include(p => p.Rates);

        return View(projectmaterials.ToList());
    }



    //
    // GET: /ProjectMaterial/Details/5

    public ViewResult Details(int id)
    {
        ProjectMaterial projectmaterial = db.ProjectMaterials.Find(id);
        return View(projectmaterial);
    }

    //
    // GET: /ProjectMaterial/Create

    public ActionResult Create()
    {
        ViewBag.ScopeID = new SelectList(db.Scopes, "ScopeID", "ScopeName");
        ViewBag.RateID = new SelectList(db.Rates, "RateID", "Unit");
        return View();
    } 

    //
    // POST: /ProjectMaterial/Create

    [HttpPost]
    public ActionResult Create(ProjectMaterial projectmaterial)
    {
        if (ModelState.IsValid)
        {
            db.ProjectMaterials.Add(projectmaterial);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        ViewBag.ScopeID = new SelectList(db.Scopes, "ScopeID", "ScopeName", projectmaterial.ScopeID);
        ViewBag.RateID = new SelectList(db.Rates, "RateID", "Unit", projectmaterial.RateID);
        return View(projectmaterial);
    }

    //
    // GET: /ProjectMaterial/Edit/5

    public ActionResult Edit(int id)
    {
        ProjectMaterial projectmaterial = db.ProjectMaterials.Find(id);
        ViewBag.ScopeID = new SelectList(db.Scopes, "ScopeID", "ScopeName", projectmaterial.ScopeID);
        ViewBag.RateID = new SelectList(db.Rates, "RateID", "Unit", projectmaterial.RateID);
        return View(projectmaterial);
    }

    //
    // POST: /ProjectMaterial/Edit/5

    [HttpPost]
    public ActionResult Edit(ProjectMaterial projectmaterial)
    {
        if (ModelState.IsValid)
        {
            db.Entry(projectmaterial).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.ScopeID = new SelectList(db.Scopes, "ScopeID", "ScopeName", projectmaterial.ScopeID);
        ViewBag.RateID = new SelectList(db.Rates, "RateID", "Unit", projectmaterial.RateID);
        return View(projectmaterial);
    }

    //
    // GET: /ProjectMaterial/Delete/5

    public ActionResult Delete(int id)
    {
        ProjectMaterial projectmaterial = db.ProjectMaterials.Find(id);
        return View(projectmaterial);
    }

    //
    // POST: /ProjectMaterial/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {            
        ProjectMaterial projectmaterial = db.ProjectMaterials.Find(id);
        db.ProjectMaterials.Remove(projectmaterial);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

Thank in advance guys !! really need your help.

+5
source share
3 answers

Seeing you say that you are new to MVC, I gave you several options and explained what is best and why, because now it’s better to understand that you don’t fall into bad habits, especially if you start building large projects.

, . , ( ):

@(Model.Quantity * Model.Rates.Amount)

, , - . ASP.NET MVC: ?.

- ViewBag, :

public ViewResult Details(int id)
{
    ProjectMaterial projectmaterial = db.ProjectMaterials.Find(id);
    ViewBag.Price = projectmaterial.Quantity * projectmaterial.Rates.Amountl
    return View(projectmaterial);
}

, :

@ViewBag.Price

, , , ViewBag - . ViewBag MVC ?.

ProjectMaterial, , .

public decimal Price
{
    get
    {
        return Quantity * Rates.Amount;
    }
}

, Price , (.. ), , , , - , - .

, - , (. http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx) . , , , , , . . , ? .

+6

ProjectMaterial:

public decimal Price
{
    get
    {
        return Quantity * Rates.Amount;
    }
}
+1

You might want to have a model function with instances of your self.rate and self.material elements passed from your views. Or else you can individually calculate the multiplication values ​​in each representation.

In any case, you should be able to store a copy over the multiplication value (variable1) in the view bag and transfer it to each view without saving to the database.

ViewBag.variable1 = rate*material
return View()
0
source

All Articles