-1

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();
     }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Cherry
  • 675
  • 3
  • 10
  • 28
  • For that you need an Ajax call. See my answer here http://stackoverflow.com/questions/20811513/model-in-layout-breaks-other-pages/20811744#20811744 – Matt Bodily Jan 04 '14 at 22:07

2 Answers2

1

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" });
Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • well! thats looking good..calling the textbox value by id..what does ?Postcode=' indicates can I use that one directly in the code itself or can i use script tag for jquery? – Cherry Jan 04 '14 at 23:47
  • that is just a part of the url, for example http://test.com/PAF/Index?Postcode=3 if you want the url formatted that way. Im sure you need to tweak it, but thats the way you do it – Greg Ennis Jan 04 '14 at 23:59
0

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>
Cherry
  • 675
  • 3
  • 10
  • 28