简体   繁体   中英

I have Button and TextBox in View of ASP.NET MVC - how to send value of Button to TextBox after Click on this Button?

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?

you are mixing up too many things.

first, if all that you want is to set text of your textbox on button click, simply use javascript ie

suppose your button is this:

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

then your js script shd be:

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

now, if you want to send a value to actionmethod in a given controller than do this:

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

where ActionMethod is like this:

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

now, if you simply want to access all the control values kept on the form, I would suggest you to use @Html.BeginForm , and provide it a proper action. So when you press the submit button kept inside it, it will raise the controller specified in the action with all the @Html controls in the formcollection ie

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

and your ActionMethod will be like:

    [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"];

In your case, above will work. You can put the above codes inside Page Load event in .cs page.

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

Request["Command"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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