' System.Web.Mvc.HtmlHelper'没有适用的方法名称' TextBox'

时间:2014-04-10 09:26:31

标签: c# asp.net asp.net-mvc

这是我在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

我在发布之前尝试了很多,没有用。 Plz的帮助。记住我是MVC的新手,这是我的第一个应用程序。

2 个答案:

答案 0 :(得分:1)

目前MVC 5已经发布,你有MVC3更完整,MVC4有很多移动功能!为什么不迁移到上一个版本?

或者请安装MVC 3 www.asp.net/mvc然后按如下方式更正您的观点

  <% using (Html.BeginForm("DisplayCustomer","Customer",FormMethod.Post))
 { %>
     Enter customer id :- <%= Html.TextBoxFor(model=>model.Id)%> <br />
     Enter customer code :- <%= Html.TextBoxFor(model=>model.CustomerCode) %><br />
     Enter customer Amount :- <%= Html.TextBoxFor(model=>model.Amount) %><br />
     <input type="submit" value="Submit customer data" />
<%} %>

答案 1 :(得分:0)

我怀疑你可能正在使用未与模型绑定的普通视图。您需要使用强类型视图将视图绑定到模型,如此处&#34;客户&#34;建模然后编写HTML帮助程序。