簡體   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