<% 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" />
<%} %>
使用Razor
@using (Html.BeginForm("DisplayResult", "DisplayCustomer", FormMethod.Post)) {
@Html.ValidationSummary(true);
@:Enter customer id :- @Html.TextBox("Id",Model)
@:Enter customer code :-@Html.TextBox("Code",Model)
@:Enter customer Amount :-@Html.TextBox("Amount",Model)
<input type="submit" value="Enter the value" />
}
编译器错误消息:CS1973:“System.Web.Mvc.HtmlHelper”没有可用的名为“TextBox”的方法,但似乎有一个同名的扩展方法。扩展方法不能动态调度。请考虑将动态参数转换或不使用扩展方法语法调用扩展方法。
@using (Html.BeginForm("DisplayResult", "DisplayCustomer", FormMethod.Post)) {
@Html.ValidationSummary(true);
@:Enter customer id :- <input type="text" name="Id" />
@:Enter customer code :-<input type="text" name="Code" />
@:Enter customer Amount :-<input type="text" name="Amount" />
<input type="submit" value="Enter the value" />
}
这个正确地运行了。
我做错了什么?我试图使用第一部分给出的代码的razor等效代码。第二部分是razor代码尝试,当只使用表单时第三部分起作用。我还想知道是否嵌套是发生错误的原因。
编辑:
@:Enter customer id :- @Html.TextBox("Id")
@:Enter customer code :-@Html.TextBox("Code")
@:Enter customer Amount :-@Html.TextBox("Amount")
如果我不使用模型,一切都可以完美运行。
控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class DisplayCustomerController : Controller
{
//
// GET: /DisplayCustomer/
[HttpPost]
public ViewResult DisplayResult()
{
Customer objCustomer = new Customer();
objCustomer.Id = Convert.ToInt16(Request.Form["Id"].ToString()); //101;
objCustomer.Amount =Convert.ToDouble(Request.Form["Amount"].ToString());// 80.00;
objCustomer.Code = Request.Form["Code"].ToString();//"IE100";
return View("DisplayResult", objCustomer);
}
public ActionResult ShowForm()
{
return View("ShowForm");
}
}
}
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
public class Customer
{
//private string Code;
// private int Id;
//private double Amount;
public String Code
{
get;
set;
}
public int Id
{
get;
set;
}
public double Amount
{
get;
set;
}
}
}
查看(展示表单)
@{
ViewBag.Title = "ShowForm";
}
<h2>ShowForm</h2>
@using (Html.BeginForm("DisplayResult", "DisplayCustomer", FormMethod.Post)) {
@Html.ValidationSummary(true);
@:Enter customer id :- @Html.TextBox("Id")
@:Enter customer code :-@Html.TextBox("Code")
@:Enter customer Amount :-@Html.TextBox("Amount")
<input type="submit" value="Submit" />
}
Model.Code
或者你需要显示的任何属性名称。我不认为这是错误的原因,但无论如何都应该修复。 - user5886152@model dynamic
吗? - user247702class MyModel
,那么在视图中添加@model yourAssembly.MyModel
。 - user3559349