Asp.net mvc 如何在ASP.NET MVC中向视图返回整数?
在下面的代码中,我试图返回一个类型为整数的视图。下面的代码不起作用。我错过了什么Asp.net mvc 如何在ASP.NET MVC中向视图返回整数?,asp.net-mvc,Asp.net Mvc,在下面的代码中,我试图返回一个类型为整数的视图。下面的代码不起作用。我错过了什么 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication5.Controllers { public class HomeController : Controller { p
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication5.Controllers
{
public class HomeController : Controller
{
public ActionResult Index(int answer)
{
int row = 2;
int col = 1;
answer = row * col ;
return View(answer);
}
}
}
你发布的代码没有问题 该错误必须与其他内容有关
如果您想知道错误的原因,可以指示您正在接收的错误以及堆栈信息。您可能在请求操作URL时没有指定应答参数。将您的方法签名更改为以下内容,应该可以正常工作:
public ActionResult Index()
然后确保视图的强类型为int:
<%@ Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<int>"
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
The answer is: <em><%= Html.DisplayForModel() %></em>
</asp:Content>
答案是:
如我所见,您试图打开URL,问题是操作“索引”有一个名为应答的参数
试着打电话,你会发现它会起作用的
编辑:
正如Darin所说,确保Index.aspx存在。您得到的错误到底是什么?这可能是由多种因素造成的,具体取决于您在应用程序中如何将路由配置为视图所在的位置/视图的定义方式。您会遇到什么错误?您是在编译时还是在尝试查看页面时遇到错误?您的视图文件夹中有名为Index.aspx的视图文件吗?它尝试了您所说的方式。编码工作正常(断点,f11),但它再次给出不同的错误。.“/”应用程序中的服务器错误。找不到视图“索引”或其主视图。搜索了以下位置:~/Views/Home/Index.aspx~/Views/Home/Index.ascx~/Views/Shared/Index.aspx~/Views/Shared/Index.ascxOkay,您需要一个关于ASP.NET MVC的教程。我建议您下载Nerd晚餐教程()并在继续之前完成它。我使用的是mvc empty webapplication。我没有任何意见。您能告诉我如何获得~/Views/Shared/Site.Master.这里的母版页是什么吗?Inherits=“System.Web.Mvc.ViewPage int”是什么意思?@软件,它表示此视图继承自通用的
System.Web.Mvc.ViewPage
类型,其中int
表示您的模型类型(返回视图时传递的类型返回视图(应答)
,应答为int
)
<%@ Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<int>"
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
The answer is: <em><%= Html.DisplayForModel() %></em>
</asp:Content>