我从来没有使用过asp.net和mvc。
我需要创建简单的注册表格,但不能将值从输入传递到控制器。我不使用模型,如果我能从视图到控制器得到值,控制器中就有服务器端函数,它将向数据库添加输入。
我搜索了很多,但总是有使用剃须刀和html.beginform等的答案,而且我没有任何答案。
以下是我的看法:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Main.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<table width="100%">
<tbody>
<tr>
<td valign="top" style="width:300px">
<fieldset>
<legend><b>Registration</b></legend>
<table class="submit">
<tr>
<td>Customer Code:</td>
<td style="width: 50%">
<%: Html.TextBox("cbCode")%>
</td>
</tr>
<tr>
<td>Card No:</td>
<td>
<%: Html.TextBox("cardNo")%>
</td>
</tr>
<tr>
<td>E-Code:</td>
<td>
<%: Html.TextBox("pswrd")%>
</td>
</tr>
<tr>
<td>E-Token:</td>
<td>
<%: Html.TextBox("tokenId")%>
</td>
</tr>
<tr>
<td>
<button type="submit" onclick="tokenSubmit('POST');" class="btn">
Submit</button>
</td>
</tr>
</table>
</fieldset>
<legend><b>Result</b></legend>
<div id="Result">
</div>
</fieldset>
</td>
</tr>
</tbody>
</table>
</asp:Content>我的控制器:
namespace Branch.Controllers
{
public class CardEcodeController : Controller
{
//
// GET: /CardEcode/
public ActionResult Index()
{
long cbCode = value from input;
long cardNo = value from input;
long tokenId = value from input;
long pswrd = value from input;
//using functions written in server side
RegisterClient reg = new RegisterClient();
reg.InsertToken(cbCode,cardNo,tokenId,pswrd);
return View();
}
}
}我认为我的MVC版本是2或3。
发布于 2014-07-17 14:06:17
我强烈建议您阅读有关使用MVC的内容。您使用的版本很可能是MVC2。如果可以的话,我将使用MVC3或MVC4进行项目,以使用Razor语法。对你问题的快速回答是这样的。
<%using (Html.BeginForm("Index", "CardEcode", FormMethod.Post)){%>
<table width="100%">
<!-- table code here -->
</table>
<% } %>对于你的控制器,你必须做一个post动作。
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(long cbCode, long cardNo, long tokenId, long pswrd)
{
//using functions written in server side
RegisterClient reg = new RegisterClient();
reg.InsertToken(cbCode,cardNo,tokenId,pswrd);
return View();
} https://stackoverflow.com/questions/24805481
复制相似问题