我在 MVC ASP.NET 视图中有按钮和文本框 - 单击此按钮后如何将按钮的值发送到文本框

本文关键字:按钮 文本 NET ASP MVC 视图 我在 单击 | 更新日期: 2023-09-27 18:35:31

我在 ASP.NET MVC View有按钮和TextBox。现在,单击此按钮后,如何将按钮的某些值(例如string x="1";)发送到文本框?我在网址地址中显示了这个值,如下所示:单击值为 1 的按钮后/local:xxxxx/Calculator?TextBox=&Command=1,但我不知道如何将此值发送到 TextBox 并转换为 int 并放入一些 int 变量。我的视图如下所示:

<input type="text" name="TextBox" value=""/>
        <table>
            <tr>
                <td><button type="submit" name="Command" value="7">7</button></td>
           </tr>

我的模型看起来像这样:

public class CModel
{
    public string Command { get; set; }
    public string TextBox { get; set; }
}

和控制器:

    [HttpGet]
    public ActionResult Index()
    {
        CModel model = new CModel();
        return View(model);
    }
    [HttpPost]
    public ActionResult Index(CModel model)
    {
        if (model.Command == "1")
        {
            //do this and this
        }
        else if(model.Command == "2")
        {
            //do this and this
        }
        else if....{}
        return View(model);
    }

有没有办法获取这个值"1"并发送到 TextBox,然后发送到 int 类型的某个变量进行保存?怎么做?

我在 MVC ASP.NET 视图中有按钮和文本框 - 单击此按钮后如何将按钮的值发送到文本框

你混淆了太多的东西。

首先,如果您只想在单击按钮时设置文本框的文本,只需使用 JavaScript 即可,即

假设你的按钮是这样的:

<input type="button" id="btnSetText" value="set Text" onclick="setText"  />

那么你的 JS 脚本 shd 是:

<script>
    function setText(){
            document.getElementById('txtBoxId').value = 'watever'; 
   }
</script>

现在,如果要将值发送到给定控制器中的ActionMethod,请执行以下操作:

  <script>
     window.href.location ='/ControllerName/ActionMethod?value=' + 1;
  </script>

其中 ActionMethod 是这样的:

public ActionResult ActionMethod(string value)
 {
       //process value
 }

现在,如果您只想访问表单上保留的所有控件值,我建议您使用 @Html.BeginForm ,并为其提供适当的操作。因此,当您按下保留在其中的提交按钮时,它将使用表单集合中的所有@Html控件来提升操作中指定的控制器,即

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true, "Login failed. Check your login details.");
    <div>
        <fieldset>
            <legend>Login</legend>
            <div class="editor-label">
                @Html.LabelFor(u => u.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(u => u.UserName)
                @Html.ValidationMessageFor(u => u.UserName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(u => u.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(u => u.Password)
                @Html.ValidationMessageFor(u => u.Password)
            </div>
            <div class="editor-label">
                @Html.CheckBoxFor(u => u.RememberMe)
                @Html.LabelFor(u => u.RememberMe)
            </div>
            <input type="submit" value="Log In" />
        </fieldset>
    </div>
}

你的操作方法将是这样的:

    [HttpPost]
    public ActionResult Login(Models.User user)
    {
        if (ModelState.IsValid)
        {
            if (user.IsValid(user.UserName, user.Password))
            {
                FormsAuthentication.SetAuthCookie(user.UserName, user.RememberMe);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Login data is incorrect!");
            }
        }
        return View(user);
    }
//this takes request parameters only from the query string 
Request.QueryString["Command"];

在您的情况下,上述内容将起作用。您可以将上述代码放在页面.cs页面加载事件中。

//this one works for bot - form and query string
Request["Command"];