【发布时间】:2014-04-10 09:26:31
【问题描述】:
这是我在 MVC2 中的第一个应用程序。以下是我的视图、控制器和另一个显示输出的视图。
StartPage.aspx
<form action="DisplayCustomer" method="post">
Enter customer id :- <input type="text" name="Id" /> <br />
Enter customer code :- <input type="text" name="CustomerCode" /><br />
Enter customer Amount :-<input type="text" name="Amount" /><br />
<input type="submit" value="Submit customer data" />
</form>
CustomerController.cs
public class CustomerController : Controller
{
[HttpPost]
public ViewResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = Convert.ToInt16(Request.Form["Id"].ToString());
objCustomer.CustomerCode = Request.Form["Id"].ToString();
objCustomer.Amount = Convert.ToDouble(Request.Form["Amount"].ToString()); ;
return View("DisplayCustomer", objCustomer);
}
}
DisplayCustomer.aspx
<div>
The customer Name is <%= Model.Name %> <br />
The customer Code is <%= Model.Code %> <br />
<% if (Model.Amount > 100) {%>
This is a privileged customer
<% } else{ %>
This is a normal customer
<%} %>
</div>
上述方法对我很有效。但我想将其更改为以下内容,就像我正在关注的教程中的那样。我还在这里包含了错误消息。
以下是我想要的样子
StartPage.aspx
<% using (Html.BeginForm("DisplayCustomer","Customer",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" />
<%} %>
错误信息
'System.Web.Mvc.HtmlHelper' 没有名为的适用方法 'TextBox' 但似乎具有该名称的扩展方法。 扩展方法不能动态调度。考虑铸造 动态参数或调用扩展方法而不 扩展方法语法。
CustomerController.cs
[HttpPost]
public ActionResult DisplayCustomer(Customer obj)
{
return View(obj);
}
我已经提供了我在此处关注的教程的链接。在该教程中,LAB5 是我一直面临的问题,位于页面底部。
http://www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?fid=1631845&df=90&mpp=25&sort=Position&spc=Relaxed&tid=4684984
在这里发帖之前我已经尝试了很多,没有用。请帮忙。请记住,我是 MVC 新手,这是我的第一个应用程序。
【问题讨论】:
-
这看起来不是一个特别好的教程(为什么不使用Razor,为什么在 TextBoxFor 是显而易见的选择时使用 TextBox)。你为什么不在asp.net/mvc上使用微软官方的@
标签: c# asp.net asp.net-mvc