繁体   English   中英

Asp.net MVC将输入值从视图传递到控制器

[英]Asp.net MVC pass input value from view to controller

我从来没有同时使用过asp.net和mvc。

我需要创建简单的注册表单,但是无法将值从输入传递到控制器。 我没有使用模型,如果我只能从视图中获取值到控制器,则控制器中有服务器端功能,它将向数据库添加输入。

我进行了很多搜索,但是使用razor和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。

我强烈建议您阅读有关使用MVC的知识 您使用的版本很可能是MVC2。 如果可以的话,我会在MVC3或MVC4中执行项目以使用Razor语法。 对您问题的快速解答是这样的。

<%using (Html.BeginForm("Index", "CardEcode", FormMethod.Post)){%>
    <table width="100%">
       <!-- table code here -->
    </table>
<% } %>

对于您的控制器,您必须进行后期操作。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM