Asp.net mvc mvc应用程序不工作
我是asp.net mvc新手。我正在使用asp.net mvc2开发一个简单的应用程序。我创建了一个控制器,它将获取用户输入并显示它。当我运行应用程序时,它会显示此错误。我的代码如下Asp.net mvc mvc应用程序不工作,asp.net-mvc,asp.net-mvc-2,Asp.net Mvc,Asp.net Mvc 2,我是asp.net mvc新手。我正在使用asp.net mvc2开发一个简单的应用程序。我创建了一个控制器,它将获取用户输入并显示它。当我运行应用程序时,它会显示此错误。我的代码如下 Server Error in '/' Application. The resource cannot be found. 控制器: [HttpPost] public ActionResult DisplayCustomer(Customer obj) { r
Server Error in '/' Application.
The resource cannot be found.
控制器:
[HttpPost]
public ActionResult DisplayCustomer(Customer obj)
{
return View("DisplayCustomer",obj);
}
<% using (Html.BeginForm("DisplayCustomer","test1",FormMethod.Post))
{ %>
Enter customer id :- <%= Html.TextBox("Id",Model)%> <br />
Enter customer code :- <%= Html.TextBox("CustomerCode",Model) %><br />
Enter customer Amount :- <%= Html.TextBox("Amount",Model) %><br />
<input type="submit" value="Submit customer data" />
<%} %>
public class Customer
{
private string _Code;
private string _Name;
private double _Amount;
public string Code
{
set
{
_Code = value;
}
get
{
return _Code;
}
}
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public double Amount
{
set
{
_Amount = value;
}
get
{
return _Amount;
}
}
}
查看:
[HttpPost]
public ActionResult DisplayCustomer(Customer obj)
{
return View("DisplayCustomer",obj);
}
<% using (Html.BeginForm("DisplayCustomer","test1",FormMethod.Post))
{ %>
Enter customer id :- <%= Html.TextBox("Id",Model)%> <br />
Enter customer code :- <%= Html.TextBox("CustomerCode",Model) %><br />
Enter customer Amount :- <%= Html.TextBox("Amount",Model) %><br />
<input type="submit" value="Submit customer data" />
<%} %>
public class Customer
{
private string _Code;
private string _Name;
private double _Amount;
public string Code
{
set
{
_Code = value;
}
get
{
return _Code;
}
}
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public double Amount
{
set
{
_Amount = value;
}
get
{
return _Amount;
}
}
}
我正在以
/test1/displaycuster
的身份运行我的应用程序。我浏览了web来解决它,但没有得到任何解决方案。请告诉我哪里出了问题。我感觉有点困惑
这样想吧:
在控制器中,在这种情况下,您必须至少有两个操作
一个(GET方法)-应该是获取页面的客户端请求并返回正确html(视图)的操作。此操作应使用[HttpGet]及其索引的默认名称(现在使用它)修饰:
[HttpGet]
public ActionResult Index()
{
return View();
}
创建此文件后,请确保创建了一个与该名称匹配的适当视图文件,在本例中为适当视图文件夹中的Index.cshtml(如果您使用的是Visual Studio,则只需右键单击控制器中的操作,然后单击“添加视图”选项即可
您应该关心的第二个操作是从页面中的表单中获取请求的操作。这个操作与您已经编写的操作非常类似
关于你的问题-
从在控制器中创建索引操作开始,创建适当的视图文件、构建和运行-应该呈现Index.cshtml
我建议您阅读一些关于HTTP GET/POST方法和用法的一般资料,以及一些关于一般MVC概念的资料(MVC是一种方法,一种使用的方式,它不仅适用于ASP.NET,而且是编程世界中的一种普遍现象)
希望有帮助。删除[HttpPost]
public ActionResult DisplayCustomer()
{
return View();
}
出于发布目的,还应包括一种行动方法:
[HttpPost]
public ActionResult DisplayCustomer(Customer obj)
{
//Code for processing post data
return View("DisplayCustomer",obj);
}
在控制器中尝试以下操作:
public ActionResult DisplayCustomer()
{
return View();
}
[HttpPost]
public ActionResult DisplayCustomer(Customer obj)
{
return RedirecttoAction("DisplayCustomer",obj);
}
在实际操作中尝试以下操作:
<% using (Html.BeginForm("DisplayCustomer",FormMethod.Post))
{ %>
Enter customer id :- <%= Html.TextBox("Id",Model)%> <br />
Enter customer code :- <%= Html.TextBox("CustomerCode",Model) %><br />``
Enter customer Amount :- <%= Html.TextBox("Amount",Model) %><br />
<input type="submit" value="Submit customer data" />
<%} %>
输入客户id:-
输入客户代码:-
``
输入客户金额:-
上述操作根本不起作用。无论我做什么,Html.TextBox(“Id”,Model)下都会有一个红色的波形。我必须删除该模型才能使其工作,我不需要第一个操作或[HttpPost],它工作得很好。[HttpPost]用于发布表单。应该还有一个操作方法Check@slacker s我想发布表单。但是您似乎没有在操作方法中处理发布的数据。无论如何,您需要另一个具有相同操作方法的操作方法name@slacker请参考此链接4:-创建简单的MVC数据输入屏幕。这就是我正在尝试的。。