Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:58:59+00:00 2026-05-28T05:58:59+00:00

Am new to programming and ASP.net MVC 3 so don’t be surprised by my

  • 0

Am new to programming and ASP.net MVC 3 so don’t be surprised by my lack of knowledge on this.. Okay, I want to multiply two decimals, One decimal comes from the form that a user fills and the other decimal comes from the Model class (gets it from the database).

I have two Model classes called RATE & PROJECTMATERIAL . The RATE class has an item called Amount that states the amount of a Rate and the PROJECTMATERIAL class has an item quantity. The classes are related and i want to be able to say variable1 = quantity*Rates.amount and return variable1 to the my Index, Delete, Details views. I don’t want to store variable1 to my database but i just want to display in my views…..but i don’t know how and where to do it

Code from 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 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");
    }

Thanx in advance guys!! really need your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:58:59+00:00Added an answer on May 28, 2026 at 5:58 am

    Seeing as you say you’re new to MVC, I’ve given you a few options and explained which is best and why, because it’s better to understand now so you don’t get in to bad habits, especially if you start building larger projects.

    You don’t necessarily need to create a variable, because you can do that calculation in your view. Because you are passing the domain model directly to the view you can do (in razor):

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

    Although this is the easiest option I wouldn’t necessarily recommend this as views should be dumb – see ASP.NET MVC: How dumb should my view be?.

    Another option is to do the calculation in the controller and pass the value in the ViewBag, e.g.:

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

    Then you could use it in your view like:

    @ViewBag.Price
    

    Again, this is easy but I wouldn’t recommend it, as ViewBag isn’t strongly typed – see Is using ViewBag in MVC bad?.

    You could put a property on your ProjectMaterial class like, which is a neat solution.

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

    However, if Price is a property that is only ever used within your views (ie you just display it) then it probably shouldn’t be in your domain model, as your domain model is just that – storing and accessing the raw data.

    Maybe the best way is to create a viewmodel specific to your view (see http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx) with a Price propert. This means that the property is only used where it is needed, the domain model remains just that, your view remains dumb and your domain model is not exposed to your view. See Why Two Classes, View Model and Domain Model? also for a good explanation of view models

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to WP7 programming and I have been following this tutorial http://weblogs.asp.net/scottgu/archive/2010/03/18/building-a-windows-phone-7-twitter-application-using-silverlight.aspx
I'm new to ASP .NET MVC and to web programming in general. I'm wondering
I am new to programming. Using C# & ASP.Net, how would I differentiate between
I am completely new to ASP.NET programming, and was asked to work on a
hello I'm new to PHP programming and I migrated from ASP .net to PHP..
I'm pretty new to web programming, reading a book on ASP.NET, and I notice
I'll admit, I am pretty new with ASP .NET programming and I have been
I'm pretty new to asp net 2.0 programming and I was wondering how can
I am relatively new to ASP.NET programming, and web programming in general. We have
I am new to ASP.NET programming. I would like to add controls to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.