0

I have Button and TextBox in View of ASP.NET MVC. Now, how to send some value (for example string x="1";) of Button to TextBox after Click on this Button? I have this value showing in url address like this: /local:xxxxx/Calculator?TextBox=&Command=1 after clicked Button with value 1 but I have no idea how to send this value to TextBox and convert to int and put in some int variable. My View look like this:

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

My model look like this:

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

And Controller:

    [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);
    }

Is there some way to get this value "1" and send to to TextBox and after that send to some variable of type int for save it? How to do that?

4

2 回答 2

4

你混淆了太多东西。

首先,如果您只想在单击按钮时设置文本框的文本,只需使用 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控制器以及 formcollection 中的所有控件,即

@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>
}

你的 ActionMethod 会是这样的:

    [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);
    }
于 2013-10-16T10:42:20.550 回答
0
//this takes request parameters only from the query string 

Request.QueryString["Command"];

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

//this one works for bot - form and query string

Request["Command"];
于 2013-10-16T10:20:20.723 回答