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 8170825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:15:38+00:00 2026-06-06T21:15:38+00:00

I’m new to MVC having previously done a lot of numerical programming in C#

  • 0

I’m new to MVC having previously done a lot of numerical programming in C# console applications. I’d like to eventually build web applications that run these same type of programs. My first attempt though has run into difficulties.

This is a very basic application that will take in a number from a form and then calculate the sum of all the numbers between 1 and that number that are divisible by either 3 or 5. I created a simple Model class which contains a variable that will correspond to the input:

namespace MvcDemo.Models
{
public  class Values 
{


    public int Number { get; set; }

}
}

I then created a controller class:

namespace MvcDemo.Controllers
{
public class HomeController : Controller
{
    Values v = new Values();


    public ActionResult Index()
    { return View(); }

    [HttpPost]
    public ActionResult Index(int number)
    {

        v.Number = number;

        return View(v); 
    }

}
}

and finally the View:

@Model MvcDemo.Models.Values

@{ViewBag.Title = "Home Page";}

<h1>Sum of Multiples of 3 & 5</h1>


@using (Html.BeginForm()){

@Html.TextBox("number");

    <input type="submit" />   
}

@{
int n;
List<int> nums = new List<int>();
int sum = 0;

if(@Model.Number!=null)
{
n = @Model.Number;
}
else
{
    n = 10;    
}

for (int i = 1; i < n; i++)
{
    if (i % 3 == 0 || i % 5 == 0)
    {
        nums.Add(i);
    }
}

for (int j = 0; j < nums.Count; j++)
{
    sum += nums[j];
}


}
<p>The sum of the factors is: @sum</p>

When I try and run this I get an exception saying “Cannot perform runtime binding on a null reference”. This is referring to my view page where I’m trying to assign n. I’m not quite sure how to get around this.

Also is it bad practice to have the numerical logic within the view class or would this be better within the controller class?

  • 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-06-06T21:15:39+00:00Added an answer on June 6, 2026 at 9:15 pm

    You’re getting an error because the model is null.

    If you use a strongly-typed view, you wouldn’t get that error, but since you wrote @Model rather than @model, your view is not strongly typed.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

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.