0

我是 asp.net mvc 的新手。我正在使用 Asp.net mvc2 开发一个简单的应用程序。我创建了一个控制器,它将获取用户输入并显示它。当我运行我的应用程序时,它向我显示了这个错误。我的代码如下。

  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;
        }
    }
}

我正在运行我的应用程序/test1/DisplayCustomer。我浏览了网络来解决它,但我没有得到任何解决方案。请让我知道我哪里出错了。

4

4 回答 4

1

删除 [HttpPost]

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

出于发布目的,还包括另一种操作方法:

 [HttpPost] 
 public ActionResult DisplayCustomer(Customer obj)
        {
           //Code for processing post data

           return View("DisplayCustomer",obj);
        }
于 2013-03-20T06:54:42.560 回答
0

在控制器中试试这个:

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" />
<%} %>
于 2013-03-20T06:59:18.430 回答
0

以上根本行不通。无论我做什么,Html.TextBox("Id", Model) 下都有一个红色波浪线。我必须删除 ,Model 才能使其工作,而且我不需要第一个操作或 [HttpPost] 并且效果很好。

于 2013-06-23T17:26:12.877 回答
0

我感觉有点混乱。

这样想:

在你的控制器中,在这种情况下,你必须至少有两个动作,

一(GET 方法)- 应该是获取客户端对 PAGE 的请求并返回正确的 html(视图)的操作。这个动作应该用 [HttpGet] 和它的默认名称(现在使用它)和它的索引来装饰:

[HttpGet]
public ActionResult Index()
{
    return View();
}

创建此文件后,请确保创建与该名称匹配的正确 View 文件,在本例中,Index.cshtml 在正确的 Views 文件夹中(如果您使用的是 Visual Studio,您只需右键单击控制器中的操作,然后然后是“添加视图”选项,它将为您执行此操作。

您应该关心的第二个操作是从页面中的表单获取请求的操作。这个和你已经写的很相似。

对于您的问题-从在控制器中创建索引操作开始,创建正确的视图文件,构建并运行-应该呈现 Index.cshtml。

我建议你阅读一些关于 HTTP GET/POST 方法和使用的一般材料,以及一些关于一般 MVC 概念的材料(MVC 是一种方法,一种使用方式——它不仅仅是 ASP.NET,它是一般的东西在编程世界中)。

希望有帮助。

于 2013-03-20T06:54:20.233 回答