i need multiplication tables output? what is the wrong?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication5.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
int row;
int col;
for (row = 1; row <= 2; row++)
{
for (col = 1; col <= 2; col++)
开发者_开发知识库 {
Console.Write(" the answer is " + row * col);
}
}
int answer = row * col;
return View(answer);
}
}
}
I want my answers to be in multiplication table. 1*1=1, 1*2=2, 2*1=2, 2*2=4 it was i want. but the above coding gives me 9 as answer. how come? what i am doing wrong here?
You probably want an IEnumerable<SomeViewModel>
as view model? In fact hard to say what you want from your question, but let me try to guess:
Model:
public MyViewModel
{
public int Col { get; set; }
public int Row { get; set; }
public int Result { get; set; }
}
Controller:
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
var model = new List<MyViewModel>();
for (int row = 1; row <= 2; row++)
for (int col = 1; col <= 2; col++)
{
model.Add(new MyViewModel
{
Row = row,
Col = col,
Result = row * col
});
}
return View(model);
}
}
View (~/Views/Home/Index.aspx
):
<%@ Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<AppName.Models.MyViewModel>>"
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% foreach (var item in Model) { %>
<div>
<span>Col: <%= item.Col %></span>
<span>Row: <%= item.Row %></span>
<span>Answer: <%= item.Result %></span>
</div>
<% } %>
</asp:Content>
Or if you use Display Templates (recommended):
<%@ Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<AppName.Models.MyViewModel>>"
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.DisplayForModel() %>
</asp:Content>
and then inside ~/Views/Home/DisplayTemplates/MyViewModel.ascx
:
<%@ Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>"
%>
<div>
<span>Col: <%= Html.DisplayFor(x => x.Col) %></span>
<span>Row: <%= Html.DisplayFor(x => x.Row) %></span>
<span>Answer: <%= Html.DisplayFor(x => x.Result) %></span>
</div>
Because it is calculated after the loop, answer
is currently the result of the multiplication of the final values of row
and col
, and ignores all the initial and intermediary values. You need to build a set of results to display, adding to that set within your loop where you currently have Console.Write(" the answer is " + row * col);
.
If you kept your iterating variables scoped to the loop, your problem and the solution would be more apparent, i.e. removing the standalone int row;
and modifying the loop statement to for (int row = 1; row <= 2; row++)
.
Within each loop, row is incremented using row++. It then checks to see if it meets the criteria (row <= 2). If it does not meet the criteria, it breaks the loop. But it has still been incremented. So when row hits 2, it runs through the loop, gets incremented to 3, and continues. Same with the col loop. So at the end of the loop, you have 3 * 3 which is 9.
- Your return value is an integer. I don't know how you expect to get a table.
9 is the correct return value for the function. I think you may not be understanding how the code you wrote works. Let me walk you through it:
int row; // The variable "row" is set to the default integer value, 0 int col=2; // The variable "cols" is set to the default integer value, 0 for (row = 1; row <= 2; row++) // set the variable row = 1. // while the value of "row" is less than or equal to 2, // at the end of the inner statement, increment the row variable by 1 { for (col = 1; col <= 2; col++) // set the variable col = 1. // while the value of "col" is less than or equal to 2, // at the end of the inner statement, increment the col variable by 1 { Console.Write(" the answer is " + row * col); }
}
So what should row and col each be equal to at the end of this statement? They are both equal to 3. Because you looped until they were equal to 2, (so during the last iteration their values were both equal to 2). However, at the end of the last iterations they were both incremented by 1, one more time, so their values are both 3.
Your return value is what is row*col. Use substitution. 3*3 = 9. There ya go.
In order to get the table values to your view, you should follow Dmitri's advice as a best practice. If you don't understand that let me try to explain it in more basic terms.
In order for your view to show a table, your view needs to accept data that is compatible with a table. There is no way to convert an integer into a table.
You want data that looks like:
1 2
1 1 2
2 2 4
So, you need in its most simple form a 2-dimensional integer array with values like:
tableArray[0] = new int[]{0,1,2};
tableArray[1] = new int[]{1,1,2};
tableArray[2] = new int[]{2,2,4};
So, the type of model your view should accept is something like a int[][]
精彩评论