我正在学习MVC,继THIS教程。 (链接将直接带你到我卡住的地方)。到目前为止,我已经了解到,每个视图都有一个控制器。现在,我必须通过教程中提到的Web入口表单来接受用户的输入。在我的项目中,我有一个名为Default1的控制器,我可以将其作为localhost:xyz/Default1/Index运行。它运行完美。在MVC中使用webforms
然后,我创建了一个新的控制器,名为Default2并将其绑定到某些视图以显示一些数据,并且它完美地作为localhost:xyz/Default2/Displaycustomer。客户信息是静态的(硬编码)。和控制器是:
public ViewResult DisplayCustomers()
{
Customer cobj = new Customer();
cobj.Code = "12";
cobj.Name = "Zeeshan";
cobj.Amount = 7000;
return View("DisplayCustomers",cobj);
}
现在我不得不采取从用户的输入,对于CUTOMER见识,使用在教程中提到html页面。所以我尝试添加新Web窗体下视图文件夹,并修改了我的控制器:
[HttpPost]
public ViewResult DisplayCustomers()
{
Customer cobj = new Customer();
cobj.Code = Request.Form["Id"].ToString();
cobj.Name = Request.Form["Name"].ToString();
cobj.Amount = Convert.ToDouble(Request.Form["Amount"].ToString());
return View("DisplayCustomers",cobj);
}
我的问题是:我怎样才能让我的项目看呆了,所以它需要输入第一,然后使用上面的控制器显示它?我是否在正确的位置添加了网页表单?运行它的链接是什么?我尝试了localhost:xyz/Default2/entryform等,但失败了。 (在我entryform.aspx,我刚才提到的形式行动=“DisplayCustomer”)
您的观点如何?当你说修改你的控制器,这是否意味着在控制器中你只有一个HttpPost方法?或者你也有一个HttpGet? – kandroid