Pregunta

I am new to mvc , I would like to know how to assign the textbox text to another variable in mvc.

In windows we can use textbox1.text = var body;

How can I do the similar one by using the below code

I want to write the text in textboxfor such that it will invoke the parameter of string postcode of PAF controller Index action?

  @Html.TextBoxFor(model => model.PostCode)

          **Here we will get : postcode: ------------(Assume Postcode:52345)**

  <input type="button" name="postcode" value="check your address" onclick="location.href='@Url.Action("Index", "PAF", new {Postcode = ...... )})'" />

            **Here we will get : check your address button**

Here is my PAF controller Index action

public class PAFController : Controller
{
     public ActionResult Index(string Postcode)
     {
        return View();
     }
}
¿Fue útil?

Solución 2

Here I got the answer for the above question: I used Jquery Ajax

 ** added data-attribute to the button**

    <input type="button" data-paf-url="/paf/index" id="postcodebutton" 
       value="check your address" /> 

    **added Id for the textbox**
   @Html.TextBoxFor(model => model.PostCode, 
       new { @class = "form-control",id="postcode",   placeholder="Postcode" })

<script>
$(document).ready(function () {
    $("#postcodebutton").click(function (e) {
        e.preventDefault();
        var postc= $("#postcode").val();
        $.ajax({
            url: $(this).attr('data-paf-url'),
            type: 'GET',
            data:{Postcode:postc},
            success: function (data, textStatus, jqXHR) {

               // my code
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('An error occured status' + textStatus + 'error thrown:' + errorThrown);
            }
        });

    });
});
</script>

Otros consejos

You need to do something such as

onclick="location.href='@Url.Action("Index", "PAF")?Postcode=' + $('#postcode').val()"

Assuming you give your input textbox an ID of postcode:

@Html.TextBoxFor(model => model.PostCode, new { id = "postcode" });
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top