簡體   English   中英

如何將文本框值傳遞給控制器

[英]How to pass textbox value to controller

我有一個如下的文本框

   @using (Html.BeginForm("checkUserType", "Place", new {type =  }))
    {
        <div id="login">
            <div class="userNameLabel">UserName</div>
            <div class="userNameField"><input type="text" id="txtUserName"/><span><input type="button" value="ok" id="ok" /></span></div>
        </div>
    }

我想將文本框值傳遞給控制器​​。 為此,我使用了下面的代碼,但是它不起作用...請幫助...

動作方式

[HttpPost]
        public ActionResult checkUserType(string type)
        {
            var elements = from element in db.USERS
                           where element.UserType == type
                           select element;
            if (elements == null)
            {
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Place/Index");
            }
        }

嘗試一次

window.location.href = '@Url.Action( "checkUserType", "Place" )?type='+type

嘗試這個

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<% Html.BeginForm("Search", "Employee");%>
<table>
   <tr>
    <td>
        Name:
    </td>
    <td>
        <input type="text" id="SearchText" name="SearchText" style="width: 325px" />
    </td>
    <td> 
    </td>
    <td>
        <input type="submit" id="Search" value="Search" />
    </td>
</tr>
</table>
<% Html.EndForm();%>

你的動作是這樣的...

public ActionResult Search(string searchText)
{ 
 }

希望對您有幫助

MVC會將您的輸入名稱映射到您的方法參數。

但是,您當前的輸入未指定名稱:

<input type="text" id="txtUserName"/>

因此添加:

<input type="text" name="type" id="txtUserName"/>

它將映射到參數。 並從BeginForm匿名對象,因為該值將由輸入字段提供,因此不必在form的動作中。

我只是更新您的代碼,請嘗試

 @using (Html.BeginForm("checkUserType", "Place"))
    {
        <div id="login">
            <div class="userNameLabel">
                UserName</div>
            <div class="userNameField">
                <input type="text" id="txtUserName" name="UserName" /><span><input type="submit" value="ok" id="ok" /></span></div>
        </div>
    }

和控制器

 public ActionResult checkUserType(string UserName)
        {
 string _data = UserName;


        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM