[英]Asp.net MVC pass input value from view to controller
I've never worked with both asp.net and mvc. 我从来没有同时使用过asp.net和mvc。
I need to create simple registration form but I can't pass value from input to controller. 我需要创建简单的注册表单,但是无法将值从输入传递到控制器。 I'm not using model and if I just could get values from view to controller there is server side function in my controller which will add inputs to database.
我没有使用模型,如果我只能从视图中获取值到控制器,则控制器中有服务器端功能,它将向数据库添加输入。
I searched a lot but there is always answers with using razor and html.beginform
etc. and I don't have any of them. 我进行了很多搜索,但是使用razor和
html.beginform
等始终可以找到答案,但我没有任何答案。
Here is my view: 这是我的看法:
<%@ 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>
My controller: 我的控制器:
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();
}
}
}
I think my MVC version is 2 or 3. 我认为我的MVC版本是2或3。
I strongly recommend you to read up on working with MVC . 我强烈建议您阅读有关使用MVC的知识 。 The version you are using is MVC2 most likely.
您使用的版本很可能是MVC2。 If you can I would do the project in MVC3 or MVC4 to use Razor syntax.
如果可以的话,我会在MVC3或MVC4中执行项目以使用Razor语法。 The quick answer to your question is this.
对您问题的快速解答是这样的。
<%using (Html.BeginForm("Index", "CardEcode", FormMethod.Post)){%>
<table width="100%">
<!-- table code here -->
</table>
<% } %>
For your controller you have to make a post action. 对于您的控制器,您必须进行后期操作。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.