mvc中的html表单动作方法中的问题 [英] problem in html form action method in mvc
问题描述
嗨朋友们
以下是我对mvc的学习示例。
在控制器类中有两种方法1.fillcustomer用于填充数据2.Displayresult显示方法。
public ActionResult Fillcustomer()
{
return View();
}
public ActionResult Displayresult()
{
customer cust = new customer();
cust.Id = Convert.ToInt32(Request.Form [CustomerId]);
cust.Name = Request.Form [Name];
cust.Amount = Convert.ToDouble(Request.Form [Amount]);
return View(cust);
}
Fillcustomer aspx文件
< body >
< 表格 action = Displayresult 方法 = post >
客户ID:< input type = text name = CustomerId / > < br / >
客户名称:< input type = text name = 名称 / > < br / >
金额:< input 类型 = text 名称 = 金额 / > < br / >
& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp ;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP; &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;
< 输入 < span class =code-attribute> type = submit value = 点击 / >
< / form >
< / body >
和displayresult aspx是
< pre lang =xml> < body >
< div >
客户ID为:<% = Model.Id %> < br / >
客户ID为:<% = Model.Name %> < br / >
客户ID为: <% = Model.Amount %> < br / >
< / div >
< / body >
当我输入控制器类的Displayresult方法它不接受时,问题出现在Fillcustomer表单操作中。可能是什么问题。
提前感谢
1。将HttpPost属性放在
public ActionResult Displayresult()
2.创建这样的表格
<%using(Html.BeginForm(actionName:ActionName,controllerName:controllerName),方法:FormMethod .Post))
{%>
客户ID:< input type =textname =CustomerId/>
客户名称:< input type =textname =Name/>
金额:< input type =textname =Amount/>
< input type =submitvalue =点击/>
<%}%>
3.Suggestioin:请使用cshtml页面和Razor syntex。
Hi friends
the following is my learning example for mvc.
In controller class two methods 1.fillcustomer for filling data 2.Displayresult method for display.
public ActionResult Fillcustomer() { return View(); } public ActionResult Displayresult() { customer cust = new customer(); cust.Id = Convert.ToInt32(Request.Form["CustomerId"]); cust.Name = Request.Form["Name"]; cust.Amount = Convert.ToDouble(Request.Form["Amount"]); return View(cust); }
Fillcustomer aspx file
<body>
<form action="Displayresult" method="post">
Customer Id: <input type="text" name="CustomerId" /><br />
Customer Name: <input type="text" name="Name" /><br />
Amount: <input type="text" name="Amount" /><br />
<input type="submit" value="Click" />
</form>
</body>
and displayresult aspx is
<body>
<div>
The Customer Id is: <%= Model.Id %><br />
The Customer Id is: <%= Model.Name %><br />
The Customer Id is: <%= Model.Amount %><br />
</div>
</body>
the problem is in Fillcustomer form action when i type Displayresult method of controller class it does not accept.what could be the problem.
thanks in advance
1. Put HttpPost attribute over
public ActionResult Displayresult()
2. Create Form Like This
<%using (Html.BeginForm(actionName:"ActionName", controllerName:"controllerName", method:FormMethod.Post))
{%>
Customer Id: <input type="text" name="CustomerId" />
Customer Name: <input type="text" name="Name" />
Amount: <input type="text" name="Amount" />
<input type="submit" value="Click" />
<%}%>
3.Suggestioin: please use cshtml pages and Razor syntex.
这篇关于mvc中的html表单动作方法中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!